diff --git a/.github/workflows/static-renderer.yml b/.github/workflows/static-renderer.yml index b2f702dee..edb49296d 100644 --- a/.github/workflows/static-renderer.yml +++ b/.github/workflows/static-renderer.yml @@ -203,6 +203,49 @@ jobs: # field and a button. See the script for what this proves and what it cannot. run: xvfb-run -a -s '-screen 0 1280x1024x24' ./.github/scripts/check-linux-input-access.sh renderer/build/linux "$RUNNER_TEMP/input-access" + - name: Parity checklist and measurements, Kotlin/Native path + if: inputs.target == 'linux-x64' + timeout-minutes: 12 + # The same checklist and the same measurements test-graalvm-renderer.yml runs on the + # GraalVM path, with the same application (the consumer fixture the step above + # built). The table is in the job summary; a row that fails is a warning, not a stop. + run: | + sudo apt-get install -y xclip + xvfb-run -a -s '-screen 0 1280x1024x24' ./scripts/parity/linux.sh native "$RUNNER_TEMP/parity" -- \ + "$PWD/target/linux-input-check/debug/consumer" --parity + + - name: Keep the parity run's logs + if: always() && inputs.target == 'linux-x64' + uses: actions/upload-artifact@v4 + with: + name: parity-native-linux + path: ${{ runner.temp }}/parity/ + if-no-files-found: ignore + retention-days: 7 + + - name: Parity measurements, Kotlin/Native macOS path + if: inputs.target == 'macos-aarch64' + timeout-minutes: 15 + # The measured half of the checklist test-graalvm-renderer.yml runs on the GraalVM + # path, with the same application: the consumer fixture, linked to the renderer built + # above. A hosted session cannot post a key from outside, so the rows that need a + # hand say n/a. The table is in the job summary. + run: | + rustup toolchain install stable --profile minimal && rustup default stable + DXC_MACOS_NATIVE_LIB="$PWD/renderer/build/macos" CARGO_TARGET_DIR="$PWD/target/parity" \ + cargo build --manifest-path compose-rust/tests/fixtures/consumer/Cargo.toml + ./scripts/parity/macos.sh native "$RUNNER_TEMP/parity" -- \ + "$PWD/target/parity/debug/consumer" --parity + + - name: Keep the macOS parity run's logs + if: always() && inputs.target == 'macos-aarch64' + uses: actions/upload-artifact@v4 + with: + name: parity-native-macos + path: ${{ runner.temp }}/parity/ + if-no-files-found: ignore + retention-days: 7 + - name: Keep the input and accessibility check's logs if: always() && inputs.target == 'linux-x64' uses: actions/upload-artifact@v4 diff --git a/.github/workflows/test-graalvm-renderer.yml b/.github/workflows/test-graalvm-renderer.yml index 99e07fe22..3bea382df 100644 --- a/.github/workflows/test-graalvm-renderer.yml +++ b/.github/workflows/test-graalvm-renderer.yml @@ -20,6 +20,7 @@ on: - 'renderer/**/module.yaml' - 'scripts/install-nik.sh' - 'scripts/tests/renderer/**' + - 'scripts/parity/**' - '.github/workflows/test-graalvm-renderer.yml' push: branches: [develop] @@ -30,6 +31,7 @@ on: - 'renderer/**/module.yaml' - 'scripts/install-nik.sh' - 'scripts/tests/renderer/**' + - 'scripts/parity/**' - '.github/workflows/test-graalvm-renderer.yml' workflow_dispatch: @@ -81,6 +83,28 @@ jobs: - name: Report the distribution size run: du -sh renderer/build/native-image/dist/lib + - name: Parity measurements, GraalVM path + timeout-minutes: 15 + # The measured half of the checklist static-renderer.yml runs on the Kotlin/Native + # path, with the same application: the consumer fixture, linked to the renderer this + # job just built. A hosted session cannot post a key from outside, so the rows that + # need a hand say n/a. The table is in the job summary. + run: | + dist="$PWD/renderer/build/native-image/dist/lib" + COMPOSE_RUST_RENDERER_DIR="$dist" CARGO_TARGET_DIR="$PWD/target/parity" \ + cargo build --manifest-path compose-rust/tests/fixtures/consumer/Cargo.toml + DYLD_LIBRARY_PATH="$dist" ./scripts/parity/macos.sh graalvm "$RUNNER_TEMP/parity" -- \ + "$PWD/target/parity/debug/consumer" --parity + + - name: Keep the parity run's logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: parity-graalvm-macos-arm64 + path: ${{ runner.temp }}/parity/ + if-no-files-found: ignore + retention-days: 7 + - name: Upload the renderer for inspection uses: actions/upload-artifact@v4 with: @@ -139,6 +163,30 @@ jobs: COMPOSE_RUST_AUTOEXIT_MS: '5000' run: xvfb-run -a ./renderer/desktop/scripts/smoke-test-linux.sh + - name: Parity checklist and measurements, GraalVM path + timeout-minutes: 15 + # The same checklist and the same measurements static-renderer.yml runs on the + # Kotlin/Native path, with the same application: the consumer fixture, linked to the + # renderer this job just built. The table is in the job summary; a row that fails is + # a warning, not a stop. + run: | + sudo apt-get install -y xdotool xclip x11-utils + dist="$PWD/renderer/build/native-image-linux/dist/lib" + COMPOSE_RUST_RENDERER_DIR="$dist" CARGO_TARGET_DIR="$PWD/target/parity" \ + cargo build --manifest-path compose-rust/tests/fixtures/consumer/Cargo.toml + LD_LIBRARY_PATH="$dist" xvfb-run -a -s '-screen 0 1280x1024x24' \ + ./scripts/parity/linux.sh graalvm "$RUNNER_TEMP/parity" -- \ + "$PWD/target/parity/debug/consumer" --parity + + - name: Keep the parity run's logs + if: always() + uses: actions/upload-artifact@v4 + with: + name: parity-graalvm-linux-x64 + path: ${{ runner.temp }}/parity/ + if-no-files-found: ignore + retention-days: 7 + - name: Upload the Linux renderer for inspection uses: actions/upload-artifact@v4 with: diff --git a/.gitignore b/.gitignore index a3bc153eb..9713178c2 100644 --- a/.gitignore +++ b/.gitignore @@ -41,6 +41,7 @@ target # needs at the time, and tracking it would mean a second lock file to update on every # dependency change, plus a dirty working tree every time the test runs. compose-rust/tests/fixtures/consumer/Cargo.lock +compose-rust/tests/fixtures/hand-check/Cargo.lock # The Host's wasm module, built next to the page by # renderer/web/scripts/build-host.sh. The loader beside it is generated and diff --git a/CHANGELOG.md b/CHANGELOG.md index 95d7bff1d..4e1d68cf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,20 @@ ## Unreleased +### Windows opens its own window, and Linux's is finished but not the default + +The Win32 window is now the default on the native-image path, with no environment +variable. The X11 window is finished the same way but Linux keeps the toolkit's window +until typing, copy and paste and Korean input have been checked on a real desktop; set +`DXC_X11_WINDOW` to try it. Windows keeps its frame and takes the caption strip into the content as before +(the logic moved from the shim into the window's own procedure), and now honours the +application's minimum size, resizable choice and icon, has a clipboard, and takes files +dropped on a drop target. Linux draws its own caption and edges and hands the move and the +resize to the window manager, has the CLIPBOARD and PRIMARY selections, text input with +composition through XIM, drag and drop of files, the application's icon, and the display +scale. The caret position is sent to the input method so candidate windows open beside it. +Not done on Linux: AT-SPI accessibility, which is being built for the Kotlin/Native window. + ### The native-image renderer opens its own window on macOS On the GraalVM native-image path, macOS now opens the AppKit window the renderer makes diff --git a/compose-rust/tests/fixtures/consumer/src/main.rs b/compose-rust/tests/fixtures/consumer/src/main.rs index ad9538e3b..4b8f974ad 100644 --- a/compose-rust/tests/fixtures/consumer/src/main.rs +++ b/compose-rust/tests/fixtures/consumer/src/main.rs @@ -24,13 +24,16 @@ //! each press of the button. That is the only way the check can tell that an event arrived //! all the way at the Host. //! +//! `--parity` is that window with a title and a smallest size of its own, for +//! `scripts/parity/linux.sh`, which reads both back from the display server. +//! //! The call to launch stays in the binary because the branch is decided at run time. //! That is what makes the renderer a load-time dependency of this executable rather than //! a library the linker drops for being unused. use compose_rust::boundary::STATUS_OK; use compose_rust::protocol::{HostEvent, Mutation, PropertyValue, ProtocolError}; -use compose_rust::schema::{EventPayload, PropertyKind, WidgetKind}; +use compose_rust::schema::{EventPayload, PropertyKind, WidgetKind, Window}; use compose_rust::{Batch, LaunchBuilder, Runtime}; use std::ffi::c_int; use std::sync::atomic::{AtomicU32, Ordering}; @@ -151,6 +154,10 @@ impl Runtime for Screen { } } +/// The smallest size `--parity` asks the window to allow, which the parity check reads back. +const PARITY_MIN_WIDTH: u16 = 300; +const PARITY_MIN_HEIGHT: u16 = 260; + const FORM: u32 = 10; const FIELD: u32 = 11; const SAVE: u32 = 12; @@ -160,12 +167,23 @@ const ON_CLICK: u64 = 2; /// A column holding a text field and a button, and nothing else. struct Form { batch: Batch, + /// Asks for a window of its own: a title and a smallest size, which the parity check reads + /// back from the display server on every path. + parity: bool, } impl Form { fn new() -> Self { Self { batch: Batch::new(), + parity: false, + } + } + + fn for_parity() -> Self { + Self { + batch: Batch::new(), + parity: true, } } @@ -196,6 +214,14 @@ impl Runtime for Form { } fn rebuild(&mut self) { + if self.parity { + self.batch.write(Mutation::SetWindow(Window { + title: "compose-rust parity", + min_width: PARITY_MIN_WIDTH, + min_height: PARITY_MIN_HEIGHT, + ..Window::new() + })); + } for (node_id, widget) in [ (FORM, WidgetKind::Column), (FIELD, WidgetKind::TextField), @@ -301,6 +327,10 @@ fn main() { LaunchBuilder::new().launch_runtime(|| Box::new(Form::new()) as Box); return; } + if std::env::args().any(|argument| argument == "--parity") { + LaunchBuilder::new().launch_runtime(|| Box::new(Form::for_parity()) as Box); + return; + } if std::env::args().any(|argument| argument == "--launch") { LaunchBuilder::new().launch_runtime(|| Box::new(Screen::new(false)) as Box); return; diff --git a/compose-rust/tests/fixtures/hand-check/Cargo.toml b/compose-rust/tests/fixtures/hand-check/Cargo.toml new file mode 100644 index 000000000..1062f8cb3 --- /dev/null +++ b/compose-rust/tests/fixtures/hand-check/Cargo.toml @@ -0,0 +1,16 @@ +# A window to check by hand on a real desktop: text shortcuts, live resize, an input method, +# focus between two fields, a click and a scrolling list with Korean in it. +# +# Like the consumer fixture next to it, this is its own workspace root, so `cargo run` works +# in this directory on Windows, macOS and Linux whatever the enclosing workspace says. It +# names its edition outright for the same reason: an excluded crate cannot inherit one. +[workspace] + +[package] +name = "hand-check" +version = "0.0.0" +edition = "2024" +publish = false + +[dependencies] +compose-rust = { path = "../../.." } diff --git a/compose-rust/tests/fixtures/hand-check/README.md b/compose-rust/tests/fixtures/hand-check/README.md new file mode 100644 index 000000000..648c070b7 --- /dev/null +++ b/compose-rust/tests/fixtures/hand-check/README.md @@ -0,0 +1,35 @@ +# hand-check + +A window to check a renderer by hand on a real desktop. It has a click counter, two text +fields, a line showing the window's size class, and a scrolling list with Korean rows. What +the fields hold is echoed under each, and every change is printed to standard output as an +`event:` line. + +Set `COMPOSE_RUST_RENDERER_DIR` to the renderer you want to try, then run it from this +directory. The commands are the same on every desktop apart from how the variable is set. + +```sh +# macOS and Linux +COMPOSE_RUST_RENDERER_DIR=/path/to/renderer/dist cargo run --release +``` + +```powershell +# Windows (PowerShell). The renderer's bin folder has to be on PATH as well. +$env:COMPOSE_RUST_RENDERER_DIR = "C:\path\to\renderer\dist" +$env:PATH = "C:\path\to\renderer\dist\bin;$env:PATH" +cargo run --release +``` + +On Linux the X11 window is opt-in until it is the default: add `DXC_X11_WINDOW=1`. + +## What to check + +| Check | How | What you should see | +| --- | --- | --- | +| Text shortcuts | In the first field select with ctrl or cmd+A, then C, X, V and Z | The echo line under the field shows the paste, the cut and the undo; `event: first ...` lines print | +| Clipboard across apps | Copy in another app, paste here, and the other way round | The text, Korean included, arrives | +| Live resize | Drag an edge and a corner, at 100% and at 200% scale | Content follows the edge with no lag or flash; the `window:` line shows the size class and size when it changes | +| Input method | Type Korean (`han-geul`) in a field | Underlined text while composing, and the candidate window beside the caret, not at the corner | +| Focus | Click each field, press Tab | Only one field has the caret, and typing goes to it | +| Click | Press the button | The counter counts and `event: click N` prints | +| Scrolling | Scroll the list with the wheel and the scrollbar | 60 rows move smoothly | diff --git a/compose-rust/tests/fixtures/hand-check/src/main.rs b/compose-rust/tests/fixtures/hand-check/src/main.rs new file mode 100644 index 000000000..c2c908ba2 --- /dev/null +++ b/compose-rust/tests/fixtures/hand-check/src/main.rs @@ -0,0 +1,189 @@ +//! A window for checking a renderer by hand. Nothing in it is a test: a person uses it and +//! reads what it says, on screen and as `event:` lines on standard output. +//! +//! What is in the window, top to bottom: +//! +//! - a label counting clicks, and the button that counts them; +//! - two text fields, to see which has the keyboard, with what each holds echoed below it, so +//! that what ctrl or cmd with C, V, X, Z and A did is on screen: a paste shows up as the new +//! text, an undo as the text going back, a cut as the text shrinking; +//! - the window's size class and size, as the renderer last reported them (it reports when +//! the class changes, not on every pixel of a resize); +//! - a scrolling list of 60 rows with Korean in them. +//! +//! For an input method, type Korean in either field and watch where the candidate window +//! opens: it should be beside the caret. +//! +//! The renderer comes from `COMPOSE_RUST_RENDERER_DIR`, as it does for any application. + +use compose_rust::protocol::{HostEvent, Mutation, PropertyValue, ProtocolError}; +use compose_rust::schema::{EventPayload, PropertyKind, WidgetKind, Window}; +use compose_rust::{Batch, LaunchBuilder, Runtime}; + +const COLUMN: u32 = 1; +const CLICKS: u32 = 2; +const BUTTON: u32 = 3; +const FIRST: u32 = 4; +const FIRST_ECHO: u32 = 5; +const SECOND: u32 = 6; +const SECOND_ECHO: u32 = 7; +const SIZE: u32 = 8; +const LIST: u32 = 9; +const ROW0: u32 = 100; + +const ON_CLICK: i64 = 1; +const ON_FIRST: i64 = 2; +const ON_SECOND: i64 = 3; + +struct App { + batch: Batch, + clicks: u32, + first: String, + second: String, + size: String, + dirty: bool, +} + +impl App { + fn text(&mut self, node_id: u32, text: &str) { + self.batch.write(Mutation::SetProp { + node_id, + property: PropertyKind::Text, + value: PropertyValue::String(text), + }); + } + + fn handler(&mut self, node_id: u32, property: PropertyKind, id: i64) { + self.batch.write(Mutation::SetProp { + node_id, + property, + value: PropertyValue::Integer(id), + }); + } +} + +impl Runtime for App { + fn batch(&self) -> &Batch { + &self.batch + } + + fn batch_mut(&mut self) -> &mut Batch { + &mut self.batch + } + + fn rebuild(&mut self) { + let mut window = Window::new(); + window.title = "compose-rust hand check"; + window.width = 480; + window.height = 700; + window.min_width = 320; + window.min_height = 320; + window.resizable = true; + self.batch.write(Mutation::SetWindow(window)); + for (node_id, widget) in [ + (COLUMN, WidgetKind::Column), + (CLICKS, WidgetKind::Text), + (BUTTON, WidgetKind::Button), + (FIRST, WidgetKind::TextField), + (FIRST_ECHO, WidgetKind::Text), + (SECOND, WidgetKind::TextField), + (SECOND_ECHO, WidgetKind::Text), + (SIZE, WidgetKind::Text), + (LIST, WidgetKind::ScrollColumn), + ] { + self.batch.write(Mutation::Create { node_id, widget }); + } + self.text(CLICKS, "clicks: 0"); + self.text(BUTTON, "Click me"); + self.text(FIRST, "first field: select, copy, paste, undo here"); + self.text(FIRST_ECHO, "first holds: first field: select, copy, paste, undo here"); + self.text(SECOND, "second field"); + self.text(SECOND_ECHO, "second holds: second field"); + self.text(SIZE, "window: not reported yet"); + self.handler(BUTTON, PropertyKind::OnClick, ON_CLICK); + self.handler(FIRST, PropertyKind::OnValueChange, ON_FIRST); + self.handler(SECOND, PropertyKind::OnValueChange, ON_SECOND); + for row in 0..60u32 { + self.batch.write(Mutation::Create { + node_id: ROW0 + row, + widget: WidgetKind::Text, + }); + self.text(ROW0 + row, &format!("List row {row} \u{d55c}\u{ae00} \u{c785}\u{b825}")); + self.batch.write(Mutation::Insert { + parent_id: LIST, + node_id: ROW0 + row, + index: row, + }); + } + for (index, node_id) in [CLICKS, BUTTON, FIRST, FIRST_ECHO, SECOND, SECOND_ECHO, SIZE, LIST] + .into_iter() + .enumerate() + { + self.batch.write(Mutation::Insert { + parent_id: COLUMN, + node_id, + index: index as u32, + }); + } + } + + fn render(&mut self) { + if !self.dirty { + return; + } + self.dirty = false; + let clicks = format!("clicks: {}", self.clicks); + let first = format!("first holds: {}", self.first); + let second = format!("second holds: {}", self.second); + let size = format!("window: {}", self.size); + self.text(CLICKS, &clicks); + self.text(FIRST_ECHO, &first); + self.text(SECOND_ECHO, &second); + self.text(SIZE, &size); + } + + fn handle_event(&mut self, event: &HostEvent<'_>) -> Result { + match (event.handler_id as i64, &event.payload) { + (ON_CLICK, EventPayload::Clicked) => { + self.clicks += 1; + println!("event: click {}", self.clicks); + } + (ON_FIRST, EventPayload::TextChanged(text)) => { + self.first = (*text).to_owned(); + println!("event: first {text}"); + } + (ON_SECOND, EventPayload::TextChanged(text)) => { + self.second = (*text).to_owned(); + println!("event: second {text}"); + } + ( + _, + EventPayload::WindowSizeChanged { + width_dp, + height_dp, + class, + .. + }, + ) => { + self.size = format!("{width_dp:.0} x {height_dp:.0} dp, {class:?}"); + println!("event: window {width_dp:.0}x{height_dp:.0} {class:?}"); + } + _ => return Err(ProtocolError::InvalidValueKind(0)), + } + self.dirty = true; + Ok(0) + } +} + +fn main() { + LaunchBuilder::new().launch_runtime(|| { + Box::new(App { + batch: Batch::new(), + clicks: 0, + first: "first field: select, copy, paste, undo here".to_owned(), + second: "second field".to_owned(), + size: "not reported yet".to_owned(), + dirty: false, + }) as Box + }); +} diff --git a/docs/design/shared-window-logic.md b/docs/design/shared-window-logic.md new file mode 100644 index 000000000..93372acfc --- /dev/null +++ b/docs/design/shared-window-logic.md @@ -0,0 +1,68 @@ +# 데스크톱 창의 공통 로직을 한 벌로 모으기 + +상태: 진행 중. 소유자 결정(2026-10-04): 데스크톱 창 코드는 구현 둘 + 공통 로직 + 일치 테스트입니다. 경로마다 OS를 부르는 얇은 층만 따로 두고, 나머지는 Kotlin 소스 한 벌(D3)입니다. + +두 경로는 이렇게 부릅니다. + +- GraalVM 경로: `renderer/desktop/c/appkit_window.m`, `win32_window.c`, `x11_window.c`와 Kotlin 래퍼(`AppKitWindow.kt`, `Win32Window.kt`, `X11Window.kt`). +- K/N 경로: `renderer/macos`, `renderer/linux`의 Kotlin cinterop 창(`MacosWindow.kt`, `LinuxWindow.kt`). Windows는 `feature/windows-kotlin-native`에 있습니다. + +## 공유 방식 + +`renderer/desktop/src/`의 파일을 `renderer/linux/src/shared/`와 `renderer/macos/src/shared/`가 심볼릭 링크로 가리키는 기존 방식을 따릅니다. 이유는 셋입니다. + +1. 이미 60개가 넘는 파일이 이 방식으로 공유되어 있어, 새 방식을 들이면 두 가지 규칙이 생깁니다. +2. desktop은 JVM 모듈이고 linux, macos는 각자 Kotlin/Native 라이브러리 모듈입니다. 공유 모듈을 따로 만들면 세 모듈의 의존과 컴파일러 옵션(opt-in, 컴파일 대상)을 새로 맞춰야 하고, 빌드를 로컬에서 돌릴 수 없는 이번 작업에서는 확인할 수 없습니다. 모듈 분리는 이 방식 위에서 나중에 할 수 있고, 지금은 링크가 이동 비용이 가장 작습니다. +3. 공유 파일은 JVM 이름(`System.getenv`, `System.nanoTime`)을 쓰는데, K/N 쪽은 `JvmCompatShims.kt`가 `java.lang`에 같은 이름을 채웁니다. 공유 파일은 그 이름만 쓰고 플랫폼 타입을 이름에 올리지 않습니다. + +## 규칙: 콜백 안에서 동기로 도는 결정은 C 헤더에 둔다 + +창이 OS 콜백 안에서 곧바로 내려야 하는 결정(크기 조절 중 그릴지 말지, 드로어블 크기, 낡은 프레임 판정)은 헤더만 있는 순수 C(`renderer/desktop/c/win32_resize.h`, `appkit_resize.h`)로 둡니다. GraalVM은 C에서 곧바로 부르고, K/N 창은 같은 헤더를 cinterop으로 부릅니다. 한 구현이고, 모달 크기 조절 루프 안에서 upcall이 없습니다. 공유 Kotlin으로 옮기는 것은 OS 콜백 밖에서 쓰는 로직(키 표, IME 상태 모델, 접근성 트리, HostTextField 규칙)만입니다. 같은 입력을 두 경로의 C 헤더에 넣는 테스트를 둡니다. + +## 중복 표 + +"공유 가능"은 Kotlin 한 벌로 합칠 수 있는가입니다. 줄 번호는 이 브랜치 기준입니다. + +| 항목 | GraalVM 경로 | K/N 경로 | 공유 가능 | 처리 | +|---|---|---|---|---| +| 키 번호 표 (X11) | `x11_window.c:214` `dxc_key_code` (C 표, 글자와 숫자 포함) | `linux/src/LinuxInput.kt:79` `platformKey` (글자와 숫자 없음) | 예 | `X11Keys.kt` 한 벌. 두 경로가 키심을 그대로 넘기고 Kotlin이 번호로 바꿉니다. K/N 쪽에 글자가 빠져 ctrl+c/v/x/z/a가 Unknown으로 가던 틈이 닫힙니다 | +| 키 번호 표 (Win32) | `WindowEvents.kt:122` `win32ComposeKey` (Kotlin, 공유 파일) | `feature/windows-kotlin-native` | 이미 공유 | 변경 없음 | +| 키 번호 표 (AppKit) | `AppKitKeys.kt:26` `composeKey`, 이 번호가 공통 번호의 기준 | `MacosWindow.kt:775`가 같은 `composeKey`를 부름 | 이미 공유 | 변경 없음 | +| 수정키 비트 | `x11_window.c:200` `dxc_modifiers` (C), `win32_window.c:1083` `dxc_held_modifiers` | `LinuxInput.kt:62` `modifiersOf` (Kotlin) | 예 (X11) | `X11Keys.kt`의 `x11Modifiers`. Win32는 값이 달라 `WindowEvents.kt`의 win32 분기가 이미 해석합니다 | +| 버튼 비트 | `x11_window.c:192` `dxc_buttons` | `LinuxInput.kt:44` `buttonsOf` | 예, 가치 낮음 | 표에 남기고 이번에는 옮기지 않았습니다 | +| IME 조합 상태 (X11) | `x11_window.c:317-375` 조합 버퍼와 콜백 (C, `wchar_t` 배열) | `InputMethod.kt:33` `PreeditBuffer`, `:75` `ImeSession` (Kotlin) | 예 | `ImeComposition.kt`로 옮기고 GraalVM C는 조합 변경(위치, 길이, 새 글자)만 넘깁니다 | +| 키 하나가 이벤트가 되는 규칙 | `x11_window.c:888-906` (키 먼저, 글자는 뒤에 commit, 단축키는 글자 없음) | `InputMethod.kt:144` `keyEventsFor` | 예 | 같은 파일로 옮기고 C는 키심, 상태, 글자만 넘깁니다 | +| IME 조합 상태 (Win32) | `win32_window.c:1140-1180` 전체 문자열을 읽어 compose/commit 이벤트를 만듭니다 | `feature/windows-kotlin-native` | 부분 | 조합 문자열이 통째로 오는 모델이라 조합 버퍼는 필요 없습니다. 이벤트 규칙은 `TextInputSession.kt`(공유)를 같이 씁니다 | +| IME 조합 상태 (AppKit) | `AppKitTextInput.kt` | `MacosWindow.kt:414,419` `insertText`, `setMarkedText` | 이미 공유 | `TextInputSession.kt`의 `compose`와 `commit`을 둘 다 씁니다 | +| 접근성 트리 만들기 | `AppKitAccessibility.kt:52` `describe()` (공유, 평평한 목록) | `AtspiSemantics.kt:59` `capture` (SemanticsNode를 AT-SPI 트리로) | 부분 | 트리 만들기는 #140이 `AtspiModel`, `AtspiSemantics`를 `desktop/src`로 옮기는 중이라 거기서 한 벌이 됩니다. 이 작업은 건드리지 않습니다 | +| 크기 조절과 프레임 결정 | `appkit_resize.h`, `win32_resize.h` (헤더 C) | `LinuxWindow.kt:356` `WindowFrames`, `ResizeSync.kt` (Kotlin, 공유) | C 헤더로 | 위 규칙대로 C 헤더가 한 벌입니다. K/N 창에 같은 헤더를 cinterop으로 붙여 같은 통계(`steps`, `stale`, `stretched`)를 냅니다 | +| 드로어블 크기, 낡은 프레임 | `appkit_window.m:1130` `dxc_resize_present`, `win32_window.c:2203` | `LinuxWindow.kt:356-362` | C 헤더로 | 위와 같습니다 | +| HostTextField 보내기 규칙 | `renderer/HostTextField.kt` | 같은 파일의 심볼릭 링크 | 이미 공유 | 변경 없음 | +| 배율과 DPI | `X11Window.kt:43`, `Win32Window.kt:92`, `AppKitWindow.kt:176` (C가 `scale`을 잼) | `LinuxWindow.kt:971` 상수 1.0, `MacosWindow.kt:652` `backingScaleFactor` | 아니오 | OS를 묻는 일이라 경로별입니다. 결과를 `WindowMeasurement`로 넘기는 모양은 이미 같습니다 | +| 클립보드 | `x11_window.c:458,497`, `appkit_window.m:228,249`, `win32_window.c:1690` | `MacosClipboard.kt`(#147에서 추가), Linux는 없음 | 부분 | 장면 쪽 `NativeClipboard.kt:28`은 이미 공유입니다. OS 호출은 경로별입니다. K/N Linux에는 클립보드 구현이 없다는 점이 일치 점검표에서 드러납니다 | +| 최소 크기, Dock 아이콘 타이밍, 시스템 다크 모드 감시, 클립보드 텍스트 규칙 | `AppKitWindow.kt`, `NativeClipboard.kt`, `SystemAppearance.kt` | `MacosWindowChrome.kt`, `MacosAppearance.kt`, `MacosClipboard.kt` | 예 | #147(`feat/macos-window-parity`)이 `WindowParity.kt` 한 벌로 옮기는 중입니다. 이 작업은 같은 것을 만들지 않고, #147이 develop에 들어온 뒤 그 파일을 Linux 창이 쓰게 합니다 | +| 창 크롬 수치 | `WindowChrome.kt:143,210` (캡션 높이, 버튼 폭) | `MacosWindow.kt:300` `measureCaption`, #147의 `MacosWindowChrome.kt` | 부분 | 최소 크기 규칙(`contentMinimum`)은 #147이 macOS 쪽에 두었습니다. 같은 규칙이 `appkit_window.m:909`에도 있어 공유 후보입니다. #147이 끝난 뒤 맞춥니다 | +| 지연 추적과 합성 입력 | `LatencyTrace.kt`, `SyntheticInput.kt` (AppKit 창에서만 썼음) | 없었음 | 예 | 두 파일이 이식 가능해졌고(`String.format` 제거, `System.nanoTime` 심) K/N Linux 창과 GraalVM X11 창이 씁니다 | + +## 일치 점검표 + +두 경로에서 같은 점검과 같은 측정을 돌립니다. 각 줄은 `scripts/parity/` 한 스크립트가 같은 표 모양(`parity <항목> <경로> <값> <단위>`)으로 출력합니다. + +- 입력 지연: `DXC_SYNTH=type`과 `DXC_REPORT_LATENCY` +- 크기 조절 프레임 시간: `DXC_SYNTH=resize`와 `DXC_REPORT_RESIZE` +- 단축키(ctrl 또는 cmd 더하기 c, v, x, z, a): 키 번호 표를 같은 벡터로 검사 +- 클립보드, 다크 모드, 최소 크기, 아이콘 + +어느 점검이 어느 CI 작업에서 도는지와 첫 수치는 PR에 적습니다. + +## 이번 작업에서 옮긴 것 + +- 키 번호 표와 수정키 비트(X11): `desktop/src/X11Keys.kt`. 두 X11 창이 키심과 상태 값을 그대로 넘기고 Kotlin이 번호로 바꿉니다. +- IME 모델(X11): `desktop/src/ImeComposition.kt`(`PreeditBuffer`, `ImeSession`, `keyEventsFor`, `candidateSpot`)와 `X11Keys.kt`의 `X11Events`. GraalVM C 창의 조합 버퍼와 키 규칙 C 코드는 없어졌고, 조합 변경(`PREEDIT_*` 이벤트)과 키심, 상태, 글자를 그대로 넘깁니다. +- 지연 추적, 합성 입력, 크기 조절 통계: `LatencyTrace.kt`, `SyntheticInput.kt`, `appkit_resize.h`를 K/N Linux 창과 GraalVM X11 창이 같이 씁니다. +- 일치 점검표: `scripts/parity/linux.sh`가 같은 앱에 같은 환경 변수와 같은 입력을 주고 한 표를 냅니다. K/N 쪽은 `static-renderer.yml`, GraalVM 쪽은 `test-graalvm-renderer.yml`에서 돕니다. + +## 동작이 바뀌는 곳 + +- K/N Linux: 글자 키가 이제 자기 키로 갑니다(이전에는 모두 알 수 없는 키). ctrl+c, v, x, z, a가 장면에 닿습니다. +- GraalVM X11: 평범한 ASCII 키는 키 이벤트 하나가 글자를 달고 갑니다(이전에는 C가 같은 글자를 commit으로도 보냈습니다). K/N 규칙으로 맞췄습니다. 실제 Linux에서 영어와 한글을 쳐 보는 확인이 필요합니다. diff --git a/renderer/desktop/c/appkit_resize.h b/renderer/desktop/c/appkit_resize.h index 1e264d15a..c3dde6cf3 100644 --- a/renderer/desktop/c/appkit_resize.h +++ b/renderer/desktop/c/appkit_resize.h @@ -50,6 +50,27 @@ static inline void dxc_resize_present(struct dxc_resize_stats *stats, int32_t wi } } +/** + * A frame whose picture is [width] by [height] was handed to the screen while the window + * measured [window_width] by [window_height], read at that moment. + * + * The criterion is that no frame is drawn at a size other than the window's current one: a + * background fill, a stretch of the old frame or a frame for a size the window has already + * left all count as stale, and the ratio of those to the frames presented has to be zero. + */ +static inline void dxc_resize_present_against(struct dxc_resize_stats *stats, int32_t width, + int32_t height, int32_t window_width, + int32_t window_height) { + stats->view_width = window_width; + stats->view_height = window_height; + dxc_resize_present(stats, width, height); +} + +/** Presented frames that were not at the window's size, as a share of all presented. */ +static inline double dxc_resize_stale_ratio(const struct dxc_resize_stats *stats) { + return stats->presented > 0 ? (double)stats->stale / (double)stats->presented : 0.0; +} + /** Sizes left without a frame, counting the last one, which has no successor to say so. */ static inline int64_t dxc_resize_stretched(const struct dxc_resize_stats *stats) { return stats->stretched + (stats->step_unanswered ? 1 : 0); diff --git a/renderer/desktop/c/appkit_window.m b/renderer/desktop/c/appkit_window.m index d10f238d2..eac703ca7 100644 --- a/renderer/desktop/c/appkit_window.m +++ b/renderer/desktop/c/appkit_window.m @@ -439,6 +439,10 @@ int32_t dxc_native_poll_event(struct dxc_event *out) { * and to the one holding focus, and a plain NSView answers none of them; everything here * turns one into a record and puts it on the queue above. */ +// Where the caret is in the scene, in pixels from the top left, for the candidate window. +static float dxc_ime_spot_x; +static float dxc_ime_spot_y; + // The paths of the files last dragged over the window, NUL separated. static NSString *dxc_dropped_paths; @@ -709,7 +713,10 @@ - (NSUInteger)characterIndexForPoint:(NSPoint)point { return NSNotFound; } // boundary; until it is asked for, the top left of the view keeps the list on screen and // near enough to read, which is better than the bottom of the display. - (NSRect)firstRectForCharacterRange:(NSRange)range actualRange:(NSRangePointer)actualRange { - NSRect local = NSMakeRect(0, 0, 1, 20); + // Where the caret is, so the input method's candidate window opens beside what is + // being typed. The renderer reports it in the scene's pixels. + CGFloat scale = self.window.backingScaleFactor > 0 ? self.window.backingScaleFactor : 1; + NSRect local = NSMakeRect(dxc_ime_spot_x / scale, dxc_ime_spot_y / scale, 1, 20); NSRect windowRect = [self convertRect:local toView:nil]; return [self.window convertRectToScreen:windowRect]; } @@ -1197,3 +1204,46 @@ void dxc_native_debug_key(void *window_pointer, int32_t key_code, const char *ch } }); } + +/** What a button of the application's own caption asks: 0 minimises, 1 zooms, 2 closes. */ +void dxc_native_window_action(int32_t action) { + dxc_on_main(^{ + @autoreleasepool { + NSWindow *window = NSApp.mainWindow ?: NSApp.windows.firstObject; + switch (action) { + case 0: [window miniaturize:nil]; break; + case 1: [window zoom:nil]; break; + case 2: [window performClose:nil]; break; + default: break; + } + } + }); +} + +/** The system moves and sizes this window itself, so nothing is asked of it. */ +void dxc_native_window_begin_drag(int32_t edge) { + (void)edge; +} + +/** Says where the caret is, in pixels from the window's top left. */ +void dxc_native_set_ime_spot(float x, float y) { + dxc_ime_spot_x = x; + dxc_ime_spot_y = y; +} + +/** + * Ends what the input method is composing and answers what it kept. Nothing here to end: this + * desktop's input method hands over whole strings and holds no composition of its own. + */ +int32_t dxc_native_ime_reset(char *out, int32_t capacity) { + (void)out; (void)capacity; + return 0; +} + +/** + * Counts a frame handed to the screen, for the resize report. The window answers for its own + * frames here, against the texture it presents, so there is nothing for the caller to add. + */ +void dxc_native_note_present(int32_t width, int32_t height) { + (void)width; (void)height; +} diff --git a/renderer/desktop/c/renderer_entry.c b/renderer/desktop/c/renderer_entry.c index ac461dbbe..bcc52210e 100644 --- a/renderer/desktop/c/renderer_entry.c +++ b/renderer/desktop/c/renderer_entry.c @@ -337,179 +337,6 @@ static void compose_rust_declare_dpi_awareness(void) { // being stretched. SetProcessDPIAware(); } - -/* - * Taking the caption strip into the client area while the frame stays whole. - * - * The obvious way to draw your own title bar is an undecorated window, and on Windows - * that is the wrong trade. The frame is not only the bar: it is the drop shadow, the - * resize border, Snap Layouts and the animation when the window is restored. None of - * those can be drawn from inside the window, and an application that gives them up looks - * worse than one that kept the system bar. The first person to run this said the borders - * looked crude, and they were. - * - * What VS Code and Windows Terminal do instead is keep every one of those and take only - * the caption. A window reports its client area in WM_NCCALCSIZE. Letting the default - * handler compute the frame and then putting the top edge back where it started leaves - * the sides and the bottom as the system's while the strip the caption occupied becomes - * ours to draw in. The styles are untouched, so the shadow, the border and Snap are - * untouched with them. - * - * Two details are not optional. A maximised window is deliberately laid out larger than - * the monitor by the border thickness, so the same edges fall off screen; restoring the - * top edge unchanged there puts the caption off screen too, and it has to be inset. And - * the top resize band lived in the non-client area that no longer exists, so the hit test - * has to answer for it or the window becomes the one window on the desktop that cannot be - * resized from the top. - * - * The geometry below is duplicated in Kotlin, which draws into the same strip. - * scripts/tests/windows-caption-metrics.test.sh fails if the two stop agreeing, because - * two numbers that have to match and live in different languages do drift. - */ - -// Windows 11 caption metrics, in device independent pixels. -#define DXC_CAPTION_HEIGHT_DIP 32 -#define DXC_CAPTION_BUTTON_WIDTH_DIP 46 -#define DXC_CAPTION_BUTTON_COUNT 3 - -static WNDPROC dxc_inner_window_proc; - -static int dxc_scale_for_window(HWND window, int dip) { - // GetDpiForWindow arrived in Windows 10 1607. Linking it would refuse to start on - // anything older, and the fallback is the desktop's own scale, which is what a - // pre-1607 machine has anyway. - typedef UINT(WINAPI * get_dpi_fn)(HWND); - UINT dpi = 0; - HMODULE user32 = LoadLibraryW(L"user32.dll"); - if (user32 != NULL) { - get_dpi_fn get_dpi = (get_dpi_fn)(void *)GetProcAddress(user32, "GetDpiForWindow"); - if (get_dpi != NULL) { - dpi = get_dpi(window); - } - FreeLibrary(user32); - } - if (dpi == 0) { - HDC screen = GetDC(NULL); - if (screen != NULL) { - dpi = (UINT)GetDeviceCaps(screen, LOGPIXELSY); - ReleaseDC(NULL, screen); - } - } - if (dpi == 0) { - dpi = 96; - } - return (int)MulDiv(dip, (int)dpi, 96); -} - -/** How far a maximised window hangs off every edge of its monitor. */ -static int dxc_maximised_overhang(HWND window) { - (void)window; - return GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER); -} - -static LRESULT CALLBACK dxc_window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) { - switch (message) { - case WM_NCCALCSIZE: { - // wparam FALSE asks only for a rectangle, with no frame to compute. - if (wparam != TRUE) { - break; - } - NCCALCSIZE_PARAMS *params = (NCCALCSIZE_PARAMS *)lparam; - LONG requested_top = params->rgrc[0].top; - CallWindowProcW(dxc_inner_window_proc, window, message, wparam, lparam); - params->rgrc[0].top = - IsZoomed(window) ? requested_top + dxc_maximised_overhang(window) : requested_top; - return 0; - } - case WM_NCHITTEST: { - LRESULT where = CallWindowProcW(dxc_inner_window_proc, window, message, wparam, lparam); - // Everywhere the frame still answers for keeps its answer: the sides, the bottom - // and all four corners are still the system's. - if (where != HTCLIENT) { - return where; - } - POINT point = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; - RECT frame; - if (!GetWindowRect(window, &frame)) { - return where; - } - // The top resize band was in the non-client area this window gave up, so nothing - // else will answer for it. - int band = dxc_maximised_overhang(window); - if (!IsZoomed(window) && point.y < frame.top + band) { - return HTTOP; - } - int caption = dxc_scale_for_window(window, DXC_CAPTION_HEIGHT_DIP); - if (point.y >= frame.top + caption) { - return HTCLIENT; - } - // The buttons are drawn by Kotlin and have to receive ordinary mouse input, so - // the strip they occupy stays client area. Everything else in the caption drags - // the window, which also brings back double click to maximise and the system - // menu on right click. - int buttons = dxc_scale_for_window( - window, DXC_CAPTION_BUTTON_WIDTH_DIP * DXC_CAPTION_BUTTON_COUNT); - if (point.x >= frame.right - buttons) { - return HTCLIENT; - } - return HTCAPTION; - } - default: - break; - } - return CallWindowProcW(dxc_inner_window_proc, window, message, wparam, lparam); -} - -struct dxc_window_search { - DWORD process; - HWND found; -}; - -static BOOL CALLBACK dxc_find_frame(HWND window, LPARAM lparam) { - struct dxc_window_search *search = (struct dxc_window_search *)lparam; - DWORD owner = 0; - GetWindowThreadProcessId(window, &owner); - if (owner != search->process || !IsWindowVisible(window)) { - return TRUE; - } - // AWT registers its top level frames under this class name. Matching on the class - // rather than on being the first visible window of the process avoids catching a - // splash, a tooltip or anything else that is ours and is not the frame. - wchar_t name[64]; - if (GetClassNameW(window, name, 64) == 0 || wcscmp(name, L"SunAwtFrame") != 0) { - return TRUE; - } - search->found = window; - return FALSE; -} - -static DWORD WINAPI dxc_reclaim_caption(LPVOID unused) { - (void)unused; - // The window is created by AWT, on AWT's thread, some time after the entry point - // returns control to Kotlin. There is no callback to wait on from here that would not - // mean a hand written bridge between Kotlin and this file, so this waits for the - // window to appear instead. Ten seconds is far longer than a window takes and short - // enough that a run which never opens one does not keep a thread forever. - for (int attempt = 0; attempt < 1000; attempt++) { - struct dxc_window_search search = {GetCurrentProcessId(), NULL}; - EnumWindows(dxc_find_frame, (LPARAM)&search); - if (search.found != NULL) { - LONG_PTR previous = - SetWindowLongPtrW(search.found, GWLP_WNDPROC, (LONG_PTR)dxc_window_proc); - if (previous == 0) { - return 1; - } - dxc_inner_window_proc = (WNDPROC)previous; - // Nothing recomputes the frame on its own, so the window is asked to. - SetWindowPos( - search.found, NULL, 0, 0, 0, 0, - SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE | SWP_FRAMECHANGED); - return 0; - } - Sleep(10); - } - return 1; -} #endif @@ -517,13 +344,6 @@ int32_t compose_rust_renderer_run(void) { #ifdef _WIN32 compose_rust_attach_parent_console(); compose_rust_declare_dpi_awareness(); - // Runs alongside the renderer because the window it waits for is created by the - // renderer. A failure here leaves an ordinary system title bar, which is a worse look - // and a working window, so it is not worth refusing to start over. - HANDLE reclaim = CreateThread(NULL, 0, dxc_reclaim_caption, NULL, 0, NULL); - if (reclaim != NULL) { - CloseHandle(reclaim); - } static LONG started; if (InterlockedExchange(&started, 1)) { return RUN_ALREADY_RUNNING; diff --git a/renderer/desktop/c/win32_window.c b/renderer/desktop/c/win32_window.c index abcf7bc6e..2130fabf7 100644 --- a/renderer/desktop/c/win32_window.c +++ b/renderer/desktop/c/win32_window.c @@ -47,6 +47,8 @@ #include #include #include +#include +#include #include #include #include @@ -75,6 +77,10 @@ enum { DXC_EVENT_KEY_UP = 6, DXC_EVENT_TEXT_COMMIT = 7, DXC_EVENT_TEXT_COMPOSE = 8, + // Files over the window, let go on it, and gone from it without being let go. + DXC_EVENT_FILES_ENTERED = 10, + DXC_EVENT_FILES_DROPPED = 11, + DXC_EVENT_FILES_EXITED = 12, }; // Room for what an input method is composing, which is a syllable or a word and never a @@ -1160,6 +1166,10 @@ static void dxc_read_ime_text(HIMC context, DWORD part, int32_t kind) { free(wide); } +// Where the caret is in the client area, in pixels, as the renderer last said. +static LONG dxc_ime_spot_x; +static LONG dxc_ime_spot_y; + static void dxc_position_ime(HWND window) { HIMC context = ImmGetContext(window); if (context == NULL) return; @@ -1168,18 +1178,173 @@ static void dxc_position_ime(HWND window) { position.dwStyle = CFS_POINT; // The caret position has not crossed from Compose yet. Keep the IME window at the // client area's top left, as the macOS text client does for the same reason. - position.ptCurrentPos.x = 0; - position.ptCurrentPos.y = 0; + position.ptCurrentPos.x = dxc_ime_spot_x; + position.ptCurrentPos.y = dxc_ime_spot_y; ImmSetCompositionWindow(context, &position); + CANDIDATEFORM candidate; + memset(&candidate, 0, sizeof candidate); + candidate.dwStyle = CFS_CANDIDATEPOS; + candidate.ptCurrentPos.x = dxc_ime_spot_x; + candidate.ptCurrentPos.y = dxc_ime_spot_y + 20; + ImmSetCandidateWindow(context, &candidate); ImmReleaseContext(window, context); } +// What the application asked of its window, held until the window is made. +static struct { + int32_t resizable; + int32_t min_width; + int32_t min_height; + int32_t system_chrome; + int32_t backdrop; +} dxc_options = {1, 0, 0, 0, 0}; + +/** + * Says how the next window should be made. Called once, before it is opened. + * + * `system_chrome` keeps the system's caption. Without it the caption strip becomes part of + * the client area and the renderer draws the title and the three buttons in it. `backdrop` + * means nothing here: this window has no material to put behind the page. + */ +void dxc_native_window_configure( + int32_t resizable, + int32_t min_width, + int32_t min_height, + int32_t system_chrome, + int32_t backdrop +) { + dxc_options.resizable = resizable; + dxc_options.min_width = min_width; + dxc_options.min_height = min_height; + dxc_options.system_chrome = system_chrome; + dxc_options.backdrop = backdrop; +} + +/** The window style the options come to. */ +static DWORD dxc_window_style(void) { + DWORD style = WS_OVERLAPPEDWINDOW; + if (!dxc_options.resizable) { + style &= ~(DWORD)(WS_THICKFRAME | WS_MAXIMIZEBOX); + } + return style; +} + +/* + * Taking the caption strip into the client area while the frame stays whole. + * + * The obvious way to draw your own title bar is an undecorated window, and on Windows + * that is the wrong trade. The frame is not only the bar: it is the drop shadow, the + * resize border, Snap Layouts and the animation when the window is restored. None of + * those can be drawn from inside the window. + * + * What VS Code and Windows Terminal do instead is keep every one of those and take only + * the caption. A window reports its client area in WM_NCCALCSIZE. Letting the default + * handler compute the frame and then putting the top edge back where it started leaves + * the sides and the bottom as the system's while the strip the caption occupied becomes + * ours to draw in. The styles are untouched, so the shadow, the border and Snap are + * untouched with them. + * + * Two details are not optional. A maximised window is deliberately laid out larger than + * the monitor by the border thickness, so the same edges fall off screen; restoring the + * top edge unchanged there puts the caption off screen too, and it has to be inset. And + * the top resize band lived in the non-client area that no longer exists, so the hit test + * has to answer for it or the window becomes the one window on the desktop that cannot be + * resized from the top. + * + * The geometry below is duplicated in Kotlin, which draws into the same strip. + * scripts/tests/windows-caption-metrics.test.sh fails if the two stop agreeing. + */ + +// Windows 11 caption metrics, in device independent pixels. +#define DXC_CAPTION_HEIGHT_DIP 32 +#define DXC_CAPTION_BUTTON_WIDTH_DIP 46 +#define DXC_CAPTION_BUTTON_COUNT 3 + +static int dxc_scaled(HWND window, int dip) { + UINT dpi = GetDpiForWindow(window); + if (dpi == 0) { + dpi = USER_DEFAULT_SCREEN_DPI; + } + return (int)MulDiv(dip, (int)dpi, USER_DEFAULT_SCREEN_DPI); +} + +/** How far a maximised window hangs off every edge of its monitor. */ +static int dxc_maximised_overhang(void) { + return GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CXPADDEDBORDER); +} + +static LRESULT dxc_caption_hit_test(HWND window, LPARAM lparam) { + LRESULT where = DefWindowProcW(window, WM_NCHITTEST, 0, lparam); + // Everywhere the frame still answers for keeps its answer: the sides, the bottom and + // all four corners are still the system's. + if (where != HTCLIENT) { + return where; + } + POINT point = {GET_X_LPARAM(lparam), GET_Y_LPARAM(lparam)}; + RECT frame; + if (!GetWindowRect(window, &frame)) { + return where; + } + // The top resize band was in the non-client area this window gave up, so nothing else + // will answer for it. A window that cannot be resized has no such band. + int band = dxc_maximised_overhang(); + if (dxc_options.resizable && !IsZoomed(window) && point.y < frame.top + band) { + return HTTOP; + } + if (point.y >= frame.top + dxc_scaled(window, DXC_CAPTION_HEIGHT_DIP)) { + return HTCLIENT; + } + // The buttons are drawn by Kotlin and have to receive ordinary mouse input, so the + // strip they occupy stays client area. Everything else in the caption drags the + // window, which also brings back double click to maximise and the system menu on + // right click. + int buttons = dxc_scaled(window, DXC_CAPTION_BUTTON_WIDTH_DIP * DXC_CAPTION_BUTTON_COUNT); + if (point.x >= frame.right - buttons) { + return HTCLIENT; + } + return HTCAPTION; +} + // Named apart from the one in `renderer_entry.c`, which subclasses the toolkit's frame // to reclaim its caption. That one goes looking for a window of AWT's class and will // not find this one, so the two never meet; the names are kept distinct anyway, // because a reader who found both would have every reason to think they were. static LRESULT CALLBACK dxc_native_window_proc(HWND window, UINT message, WPARAM wparam, LPARAM lparam) { switch (message) { + case WM_NCCALCSIZE: { + if (dxc_options.system_chrome || wparam != TRUE) { + break; + } + NCCALCSIZE_PARAMS *params = (NCCALCSIZE_PARAMS *)lparam; + LONG requested_top = params->rgrc[0].top; + DefWindowProcW(window, message, wparam, lparam); + params->rgrc[0].top = IsZoomed(window) ? requested_top + dxc_maximised_overhang() + : requested_top; + return 0; + } + case WM_NCHITTEST: + if (dxc_options.system_chrome) { + break; + } + return dxc_caption_hit_test(window, lparam); + case WM_GETMINMAXINFO: + if (dxc_options.min_width > 0 || dxc_options.min_height > 0) { + UINT dpi = GetDpiForWindow(window); + if (dpi == 0) { + dpi = USER_DEFAULT_SCREEN_DPI; + } + RECT wanted = {0, 0, MulDiv(dxc_options.min_width, (int)dpi, USER_DEFAULT_SCREEN_DPI), + MulDiv(dxc_options.min_height, (int)dpi, USER_DEFAULT_SCREEN_DPI)}; + AdjustWindowRectExForDpi(&wanted, dxc_window_style(), FALSE, 0, dpi); + MINMAXINFO *info = (MINMAXINFO *)lparam; + info->ptMinTrackSize.x = wanted.right - wanted.left; + // The caption strip is client area here, so the top of the frame is not + // outside the content. + info->ptMinTrackSize.y = dxc_options.system_chrome ? wanted.bottom - wanted.top + : wanted.bottom; + return 0; + } + break; case WM_MOUSEMOVE: // No tracking area. Windows delivers a move whenever the pointer is over the // client area, so hover, which is half of what a desktop control does, arrives @@ -1424,6 +1589,295 @@ void dxc_native_install_menu(const char *application_name) { (void)application_name; } + +/** The caption strip is fixed on this platform and Kotlin knows its height, so nothing is measured. */ +void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { + (void)view_pointer; + *height = 0; + *buttons_width = 0; +} + +/** + * Does with the window what a button of the application's own caption asks: 0 minimises, + * 1 maximises or restores, 2 closes. + */ +void dxc_native_window_action(int32_t action) { + if (dxc_window == NULL) { + return; + } + switch (action) { + case 0: + ShowWindow(dxc_window, SW_MINIMIZE); + break; + case 1: + ShowWindow(dxc_window, IsZoomed(dxc_window) ? SW_RESTORE : SW_MAXIMIZE); + break; + case 2: + PostMessageW(dxc_window, WM_CLOSE, 0, 0); + break; + default: + break; + } +} + +/** The system moves the window itself where the caption is the system's, so nothing here. */ +void dxc_native_window_begin_drag(int32_t edge) { + (void)edge; +} + +/** + * Gives the window the picture it named, from its pixels: eight bits each of red, green, + * blue and alpha, with the colour already multiplied by the alpha, row after row. + */ +void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { + if (dxc_window == NULL || rgba == NULL || width <= 0 || height <= 0) { + return; + } + BITMAPV5HEADER header; + memset(&header, 0, sizeof header); + header.bV5Size = sizeof header; + header.bV5Width = width; + // Negative: the rows are top first. + header.bV5Height = -height; + header.bV5Planes = 1; + header.bV5BitCount = 32; + header.bV5Compression = BI_BITFIELDS; + header.bV5RedMask = 0x00FF0000; + header.bV5GreenMask = 0x0000FF00; + header.bV5BlueMask = 0x000000FF; + header.bV5AlphaMask = 0xFF000000; + void *bits = NULL; + HDC screen = GetDC(NULL); + HBITMAP color = CreateDIBSection(screen, (BITMAPINFO *)&header, DIB_RGB_COLORS, &bits, NULL, 0); + ReleaseDC(NULL, screen); + if (color == NULL || bits == NULL) { + if (color != NULL) DeleteObject(color); + return; + } + uint8_t *out = (uint8_t *)bits; + for (int64_t index = 0; index < (int64_t)width * height; index++) { + uint8_t r = rgba[index * 4 + 0]; + uint8_t g = rgba[index * 4 + 1]; + uint8_t b = rgba[index * 4 + 2]; + uint8_t a = rgba[index * 4 + 3]; + // An icon's alpha is straight, so the colour is divided back out. + if (a != 0 && a != 255) { + r = (uint8_t)((r * 255 + a / 2) / a); + g = (uint8_t)((g * 255 + a / 2) / a); + b = (uint8_t)((b * 255 + a / 2) / a); + } + out[index * 4 + 0] = b; + out[index * 4 + 1] = g; + out[index * 4 + 2] = r; + out[index * 4 + 3] = a; + } + HBITMAP mask = CreateBitmap(width, height, 1, 1, NULL); + ICONINFO info; + memset(&info, 0, sizeof info); + info.fIcon = TRUE; + info.hbmMask = mask; + info.hbmColor = color; + HICON icon = CreateIconIndirect(&info); + DeleteObject(color); + DeleteObject(mask); + if (icon != NULL) { + SendMessageW(dxc_window, WM_SETICON, ICON_BIG, (LPARAM)icon); + SendMessageW(dxc_window, WM_SETICON, ICON_SMALL, (LPARAM)icon); + } +} + +/** What is on the clipboard as text, copied into [out] as UTF-8, and its length. */ +int32_t dxc_native_clipboard_read(char *out, int32_t capacity) { + if (!OpenClipboard(dxc_window)) { + return 0; + } + int32_t length = 0; + HANDLE data = GetClipboardData(CF_UNICODETEXT); + if (data != NULL) { + const wchar_t *wide = (const wchar_t *)GlobalLock(data); + if (wide != NULL) { + int written = WideCharToMultiByte(CP_UTF8, 0, wide, -1, out, capacity, NULL, NULL); + // The count includes the terminator, and is zero where the text did not fit. + if (written > 0) { + length = written - 1; + } + GlobalUnlock(data); + } + } + CloseClipboard(); + return length; +} + +/** Replaces the clipboard's contents with [text], which is UTF-8. */ +void dxc_native_clipboard_write(const char *text) { + int units = MultiByteToWideChar(CP_UTF8, 0, text, -1, NULL, 0); + if (units <= 0 || !OpenClipboard(dxc_window)) { + return; + } + HGLOBAL memory = GlobalAlloc(GMEM_MOVEABLE, (SIZE_T)units * sizeof(wchar_t)); + if (memory != NULL) { + wchar_t *wide = (wchar_t *)GlobalLock(memory); + if (wide != NULL) { + MultiByteToWideChar(CP_UTF8, 0, text, -1, wide, units); + GlobalUnlock(memory); + EmptyClipboard(); + // The clipboard owns the memory from here on. + if (SetClipboardData(CF_UNICODETEXT, memory) == NULL) { + GlobalFree(memory); + } + } else { + GlobalFree(memory); + } + } + CloseClipboard(); +} + +/* + * Files dragged over the window, through OLE, which is what says where they are and when + * they leave. WM_DROPFILES says only that they were dropped. + */ +#define DXC_DROPPED_BYTES (64 * 1024) +static char dxc_dropped_paths[DXC_DROPPED_BYTES]; +static int32_t dxc_dropped_length; +static int dxc_drag_has_files; + +/** The paths of the files last dragged over the window, NUL between them, and the length. */ +int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { + if (dxc_dropped_length <= 0 || dxc_dropped_length > capacity) { + return 0; + } + memcpy(out, dxc_dropped_paths, (size_t)dxc_dropped_length); + return dxc_dropped_length; +} + +static FORMATETC dxc_hdrop_format(void) { + FORMATETC format; + format.cfFormat = CF_HDROP; + format.ptd = NULL; + format.dwAspect = DVASPECT_CONTENT; + format.lindex = -1; + format.tymed = TYMED_HGLOBAL; + return format; +} + +static void dxc_read_dragged_files(IDataObject *data) { + dxc_dropped_length = 0; + FORMATETC format = dxc_hdrop_format(); + STGMEDIUM medium; + if (FAILED(IDataObject_GetData(data, &format, &medium))) { + return; + } + HDROP drop = (HDROP)medium.hGlobal; + UINT count = DragQueryFileW(drop, 0xFFFFFFFFu, NULL, 0); + int32_t used = 0; + for (UINT index = 0; index < count; index++) { + wchar_t wide[MAX_PATH * 4]; + UINT units = DragQueryFileW(drop, index, wide, (UINT)(sizeof wide / sizeof *wide)); + if (units == 0) continue; + int bytes = WideCharToMultiByte(CP_UTF8, 0, wide, (int)units, NULL, 0, NULL, NULL); + // Room for the separator as well, and a path that does not fit is left out rather + // than cut in half. + if (bytes <= 0 || used + bytes + 1 > DXC_DROPPED_BYTES) continue; + if (used > 0) dxc_dropped_paths[used++] = '\0'; + WideCharToMultiByte(CP_UTF8, 0, wide, (int)units, dxc_dropped_paths + used, bytes, NULL, NULL); + used += bytes; + } + dxc_dropped_length = used; + ReleaseStgMedium(&medium); +} + +static void dxc_push_drag(int32_t kind, POINTL where) { + POINT point = {where.x, where.y}; + if (dxc_window != NULL) { + ScreenToClient(dxc_window, &point); + } + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = kind; + record.x = (float)point.x; + record.y = (float)point.y; + dxc_push_event(record); +} + +static HRESULT STDMETHODCALLTYPE dxc_drop_query(IDropTarget *self, REFIID id, void **out) { + if (IsEqualIID(id, &IID_IUnknown) || IsEqualIID(id, &IID_IDropTarget)) { + *out = self; + return S_OK; + } + *out = NULL; + return E_NOINTERFACE; +} +static ULONG STDMETHODCALLTYPE dxc_drop_add_ref(IDropTarget *self) { (void)self; return 1; } +static ULONG STDMETHODCALLTYPE dxc_drop_release(IDropTarget *self) { (void)self; return 1; } + +static HRESULT STDMETHODCALLTYPE dxc_drop_enter( + IDropTarget *self, IDataObject *data, DWORD keys, POINTL where, DWORD *effect) { + (void)self; + (void)keys; + FORMATETC format = dxc_hdrop_format(); + dxc_drag_has_files = SUCCEEDED(IDataObject_QueryGetData(data, &format)); + if (!dxc_drag_has_files) { + *effect = DROPEFFECT_NONE; + return S_OK; + } + dxc_read_dragged_files(data); + dxc_push_drag(DXC_EVENT_FILES_ENTERED, where); + *effect = DROPEFFECT_COPY; + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE dxc_drop_over( + IDropTarget *self, DWORD keys, POINTL where, DWORD *effect) { + (void)self; + (void)keys; + if (!dxc_drag_has_files) { + *effect = DROPEFFECT_NONE; + return S_OK; + } + dxc_push_drag(DXC_EVENT_FILES_ENTERED, where); + *effect = DROPEFFECT_COPY; + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE dxc_drop_leave(IDropTarget *self) { + (void)self; + if (dxc_drag_has_files) { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_FILES_EXITED; + dxc_push_event(record); + } + dxc_drag_has_files = 0; + return S_OK; +} + +static HRESULT STDMETHODCALLTYPE dxc_drop_drop( + IDropTarget *self, IDataObject *data, DWORD keys, POINTL where, DWORD *effect) { + (void)self; + (void)keys; + if (!dxc_drag_has_files) { + *effect = DROPEFFECT_NONE; + return S_OK; + } + dxc_read_dragged_files(data); + dxc_push_drag(DXC_EVENT_FILES_DROPPED, where); + dxc_drag_has_files = 0; + *effect = DROPEFFECT_COPY; + return S_OK; +} + +static IDropTargetVtbl dxc_drop_vtable = { + dxc_drop_query, dxc_drop_add_ref, dxc_drop_release, + dxc_drop_enter, dxc_drop_over, dxc_drop_leave, dxc_drop_drop, +}; +static IDropTarget dxc_drop_target = {&dxc_drop_vtable}; + +/** Lets the window be dropped on. Drag and drop is OLE's, which needs a single threaded apartment. */ +static void dxc_accept_files(HWND window) { + OleInitialize(NULL); + RegisterDragDrop(window, &dxc_drop_target); +} + /* * Measuring aids of the AppKit window, named because the Kotlin that drives every desktop * names them and answered here by doing nothing: a drag cannot be scripted from inside @@ -1449,53 +1903,6 @@ void dxc_native_debug_key(void *window_pointer, int32_t key_code, const char *ch * ones that do not, which is a defect that travels to whoever builds for the strictest * platform. It travelled three times before this was written down. */ -int32_t dxc_native_clipboard_read(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; -} - -void dxc_native_clipboard_write(const char *text) { - (void)text; -} - -/* - * Asked for by name by the window code every desktop shares, and answered here by saying - * nothing: this window takes its options another way, has no strip of the system's to - * measure, and reads no list of dropped paths from the platform. - */ -void dxc_native_window_configure( - int32_t resizable, - int32_t min_width, - int32_t min_height, - int32_t system_chrome, - int32_t backdrop -) { - (void)resizable; - (void)min_width; - (void)min_height; - (void)system_chrome; - (void)backdrop; -} - -void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { - (void)view_pointer; - *height = 0; - *buttons_width = 0; -} - -void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { - (void)rgba; - (void)width; - (void)height; -} - -int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; -} - void dxc_native_set_frame_callback(void *callback, void *isolate_thread) { (void)callback; (void)isolate_thread; @@ -1648,7 +2055,7 @@ int32_t dxc_native_window_open( 0, DXC_WINDOW_CLASS, wide_title, - WS_OVERLAPPEDWINDOW, + dxc_window_style(), CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, GetModuleHandleW(NULL), NULL); if (window == NULL) { @@ -1667,9 +2074,11 @@ int32_t dxc_native_window_open( wanted.top = 0; wanted.right = MulDiv(width, (int)dpi, USER_DEFAULT_SCREEN_DPI); wanted.bottom = MulDiv(height, (int)dpi, USER_DEFAULT_SCREEN_DPI); - AdjustWindowRectExForDpi(&wanted, WS_OVERLAPPEDWINDOW, FALSE, 0, dpi); + AdjustWindowRectExForDpi(&wanted, dxc_window_style(), FALSE, 0, dpi); int outer_width = wanted.right - wanted.left; - int outer_height = wanted.bottom - wanted.top; + // Without the system caption the content starts at the top of the window, so the + // frame above it is not part of the outside. + int outer_height = dxc_options.system_chrome ? wanted.bottom - wanted.top : wanted.bottom; // Centred on the part of the screen a window is meant to sit in. Where that cannot be // asked for, the window keeps the place Windows chose for it rather than being moved // to a corner that was never a position. @@ -1682,7 +2091,7 @@ int32_t dxc_native_window_open( top = work.top + ((work.bottom - work.top) - outer_height) / 2; placement = SWP_NOZORDER; } - SetWindowPos(window, NULL, left, top, outer_width, outer_height, placement); + SetWindowPos(window, NULL, left, top, outer_width, outer_height, placement | SWP_FRAMECHANGED); RECT client; GetClientRect(window, &client); @@ -1748,6 +2157,12 @@ int32_t dxc_native_window_open( swapchain_description.SampleDesc.Count = 1; swapchain_description.SwapEffect = DXGI_SWAP_EFFECT_FLIP_DISCARD; swapchain_description.AlphaMode = DXGI_ALPHA_MODE_IGNORE; + // Left at its zero value this is DXGI_SCALING_STRETCH, which fills the client area + // from whatever the back buffer holds. A frame drawn at the size just given always + // matches, so this never shows on an ordinary resize, but a frame skipped by + // dxc_native_frame_begin (a refit refused, or the swapchain not yet made) would be + // the previous buffer stretched to the new area instead of left at its own size. + swapchain_description.Scaling = DXGI_SCALING_NONE; IDXGISwapChain1 *first = NULL; HRESULT made = IDXGIFactory4_CreateSwapChainForHwnd( factory, (IUnknown *)queue, window, &swapchain_description, NULL, NULL, &first); @@ -1815,6 +2230,7 @@ int32_t dxc_native_window_open( return 11; } + dxc_accept_files(window); ShowWindow(window, SW_SHOW); SetForegroundWindow(window); SetFocus(window); @@ -1943,3 +2359,29 @@ void dxc_native_frame_end(void *queue_pointer) { // swapchain two buffers deep comes back around immediately. dxc_wait_for_gpu(); } + +/** Says where the caret is, in pixels from the window's top left. */ +void dxc_native_set_ime_spot(float x, float y) { + dxc_ime_spot_x = (LONG)x; + dxc_ime_spot_y = (LONG)y; + if (dxc_window != NULL && dxc_ime_composing) { + dxc_position_ime(dxc_window); + } +} + +/** + * Ends what the input method is composing and answers what it kept. Nothing here to end: this + * desktop's input method hands over whole strings and holds no composition of its own. + */ +int32_t dxc_native_ime_reset(char *out, int32_t capacity) { + (void)out; (void)capacity; + return 0; +} + +/** + * Counts a frame handed to the screen, for the resize report. The window answers for its own + * frames here, against the texture it presents, so there is nothing for the caller to add. + */ +void dxc_native_note_present(int32_t width, int32_t height) { + (void)width; (void)height; +} diff --git a/renderer/desktop/c/x11_window.c b/renderer/desktop/c/x11_window.c index 6324ef9c2..01bd76540 100644 --- a/renderer/desktop/c/x11_window.c +++ b/renderer/desktop/c/x11_window.c @@ -5,9 +5,12 @@ #include #include #include +#include #include #include #include +#include "appkit_resize.h" +#include #include #include #include @@ -22,10 +25,19 @@ enum { DXC_EVENT_SCROLL = 4, DXC_EVENT_KEY_DOWN = 5, DXC_EVENT_KEY_UP = 6, - // XIM, ibus and fcitx text composition is separate work. These kinds are declared - // for the shared reader, but this window does not populate text events. + // Text the input method finished, and text it is still composing. DXC_EVENT_TEXT_COMMIT = 7, DXC_EVENT_TEXT_COMPOSE = 8, + // What the input method's preedit callbacks said, passed on as they were said: a + // composition began, a run of characters was replaced, the composition ended. What the + // composition holds is kept on the Kotlin side, in the model the other X11 window uses. + DXC_EVENT_PREEDIT_START = 13, + DXC_EVENT_PREEDIT_DRAW = 14, + DXC_EVENT_PREEDIT_DONE = 15, + // Files over the window, let go on it, and gone from it without being let go. + DXC_EVENT_FILES_ENTERED = 10, + DXC_EVENT_FILES_DROPPED = 11, + DXC_EVENT_FILES_EXITED = 12, }; #define DXC_TEXT_BYTES 96 @@ -128,6 +140,64 @@ static int32_t dxc_cursor_shape = -1; static struct dxc_element dxc_elements[DXC_ELEMENT_CAPACITY]; static int32_t dxc_element_count; +// What the application asked of its window, held until the window is made. +static struct { + int32_t resizable; + int32_t min_width; + int32_t min_height; + int32_t system_chrome; + int32_t backdrop; +} dxc_options = {1, 0, 0, 0, 0}; + +// How many pixels go to a point on this display, read once when the window is made. +static float dxc_scale = 1.0f; + +// Atoms this file asks the server for once. +static Atom dxc_a_clipboard, dxc_a_primary, dxc_a_utf8, dxc_a_targets, dxc_a_text, + dxc_a_property, dxc_a_net_wm_name, dxc_a_net_wm_icon, dxc_a_net_wm_state, + dxc_a_maximized_vert, dxc_a_maximized_horz, dxc_a_moveresize, dxc_a_active_window, + dxc_a_motif_hints, dxc_a_incr; +static Atom dxc_a_xdnd_aware, dxc_a_xdnd_enter, dxc_a_xdnd_position, dxc_a_xdnd_status, + dxc_a_xdnd_leave, dxc_a_xdnd_drop, dxc_a_xdnd_finished, dxc_a_xdnd_selection, + dxc_a_xdnd_copy, dxc_a_xdnd_type_list, dxc_a_uri_list; + +// The text this window put on the clipboard, which it answers for while it owns it. +static char *dxc_clip_text; +static size_t dxc_clip_length; +static int dxc_owns_clipboard; + +// Set from any thread to ask the window to come forward; acted on in the next turn, which +// is on the thread Xlib is used from. +static volatile int dxc_raise_requested; + +// The input method and the context made from it. +// What a resize cost and what it showed, counted by the header the AppKit window and the +// Kotlin/Native windows count with, and reported at exit when asked for with +// DXC_REPORT_RESIZE. A stretched step is a size the window took that was left without a frame. +static struct dxc_resize_stats dxc_resize_stats; + +static void dxc_resize_report(void) { + fprintf(stderr, + "dxc resize: steps=%lld presented=%lld stale=%lld stretched=%lld stale_ratio=%.4f\n", + (long long)dxc_resize_stats.steps, (long long)dxc_resize_stats.presented, + (long long)dxc_resize_stats.stale, (long long)dxc_resize_stretched(&dxc_resize_stats), + dxc_resize_stale_ratio(&dxc_resize_stats)); +} + +static XIM dxc_im; +static XIC dxc_ic; +#define DXC_PREEDIT_CAPACITY 256 + +// Files being dragged over the window and the ones last let go on it. +#define DXC_DROPPED_BYTES (64 * 1024) +static char dxc_dropped_paths[DXC_DROPPED_BYTES]; +static int32_t dxc_dropped_length; +static Window dxc_drag_source; +static int dxc_drag_has_files; +static Time dxc_drop_time; +// Where the last XdndPosition said the files were, in root coordinates. +static int dxc_drag_root_x, dxc_drag_root_y; + static void dxc_push_event(struct dxc_event event) { if (dxc_event_count == DXC_EVENT_CAPACITY) { dxc_event_head = (dxc_event_head + 1) % DXC_EVENT_CAPACITY; @@ -143,36 +213,9 @@ static int32_t dxc_buttons(unsigned int state) { ((state & Button2Mask) ? 4 : 0); } -// The shared Compose reader uses these four bits, as NSEvent does. Translate at the -// boundary so the same reader receives the same meaning on every desktop. -static int32_t dxc_modifiers(unsigned int state) { - return ((state & ShiftMask) ? (1 << 17) : 0) | - ((state & ControlMask) ? (1 << 18) : 0) | - ((state & Mod1Mask) ? (1 << 19) : 0) | - ((state & Mod4Mask) ? (1 << 20) : 0); -} - -// The special key values are the shared reader's values. Printable keys also carry -// their code point, obtained from XLookupString for the current keyboard layout. -static int32_t dxc_key_code(KeySym key) { - switch (key) { - case XK_Return: case XK_KP_Enter: return 0x24; - case XK_Tab: case XK_ISO_Left_Tab: return 0x30; - case XK_space: return 0x31; - case XK_BackSpace: return 0x33; - case XK_Escape: return 0x35; - case XK_Delete: return 0x75; - case XK_Left: return 0x7B; - case XK_Right: return 0x7C; - case XK_Down: return 0x7D; - case XK_Up: return 0x7E; - case XK_Home: return 0x73; - case XK_End: return 0x77; - case XK_Prior: return 0x74; - case XK_Next: return 0x79; - default: return 0; - } -} +// Keys and modifiers cross as the server wrote them: a keysym and a state word. Turning +// them into what the shared reader understands is Kotlin's, in one table both X11 windows +// ask (`X11Keys.kt`). /** * Tells the window manager that the drawing it was waiting for is done. @@ -218,14 +261,546 @@ void dxc_native_set_frame_callback(int32_t (*callback)(void *isolate_thread), vo dxc_frame_thread = isolate_thread; } +/* Encodes one code point as UTF-8 and answers how many bytes that was. */ +static int dxc_encode_utf8(unsigned long code, char *out) { + if (code < 0x80) { out[0] = (char)code; return 1; } + if (code < 0x800) { + out[0] = (char)(0xC0 | (code >> 6)); out[1] = (char)(0x80 | (code & 0x3F)); return 2; + } + if (code < 0x10000) { + out[0] = (char)(0xE0 | (code >> 12)); out[1] = (char)(0x80 | ((code >> 6) & 0x3F)); + out[2] = (char)(0x80 | (code & 0x3F)); return 3; + } + out[0] = (char)(0xF0 | (code >> 18)); out[1] = (char)(0x80 | ((code >> 12) & 0x3F)); + out[2] = (char)(0x80 | ((code >> 6) & 0x3F)); out[3] = (char)(0x80 | (code & 0x3F)); + return 4; +} + +/* + * The input method's preedit callbacks. Each is passed on as an event and nothing is kept + * here: the method says which run of characters changed and what they are now, counted in + * characters as the protocol counts them, and the model that applies that to the run being + * composed is the Kotlin one (`ImeComposition.kt`), which the Kotlin/Native window uses too. + */ +static int dxc_preedit_start(XIC ic, XPointer client, XPointer call) { + (void)ic; (void)client; (void)call; + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_PREEDIT_START; + dxc_push_event(record); + // No limit on the length of what is composed. The method reads this return value, and + // a function that returns nothing would leave whatever was in the register. + return -1; +} + +static void dxc_preedit_draw(XIC ic, XPointer client, XIMPreeditDrawCallbackStruct *draw) { + (void)ic; (void)client; + if (draw == NULL) return; + // The characters the method put in, as UTF-8. Decoding what Xlib handed over is this + // file's job; what the characters mean for the composition is not. + char utf8[DXC_PREEDIT_CAPACITY * 4 + 1]; + size_t used = 0; + XIMText *text = draw->text; + if (text != NULL) { + if (text->encoding_is_wchar && text->string.wide_char != NULL) { + for (int i = 0; i < (int)text->length && i < DXC_PREEDIT_CAPACITY; i++) { + used += (size_t)dxc_encode_utf8((unsigned long)text->string.wide_char[i], utf8 + used); + } + } else if (text->string.multi_byte != NULL) { + wchar_t added[DXC_PREEDIT_CAPACITY]; + size_t count = mbstowcs(added, text->string.multi_byte, DXC_PREEDIT_CAPACITY); + if (count != (size_t)-1) { + for (size_t i = 0; i < count; i++) { + used += (size_t)dxc_encode_utf8((unsigned long)added[i], utf8 + used); + } + } + } + } + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_PREEDIT_DRAW; + record.key_code = draw->chg_first; + record.code_point = draw->chg_length; + record.x = (float)draw->caret; + size_t take = used < DXC_TEXT_BYTES - 1 ? used : DXC_TEXT_BYTES - 1; + while (take > 0 && take < used && ((unsigned char)utf8[take] & 0xC0) == 0x80) { + take--; + } + memcpy(record.text, utf8, take); + dxc_push_event(record); +} + +static void dxc_preedit_done(XIC ic, XPointer client, XPointer call) { + (void)ic; (void)client; (void)call; + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_PREEDIT_DONE; + dxc_push_event(record); +} + +static void dxc_preedit_caret(XIC ic, XPointer client, XPointer call) { + (void)ic; (void)client; (void)call; +} + +static XIMCallback dxc_cb_start, dxc_cb_draw, dxc_cb_done, dxc_cb_caret; + +/* Opens the desktop's input method and a context on the window. None is not an error. */ +static void dxc_open_input_method(void) { + setlocale(LC_CTYPE, ""); + if (!XSupportsLocale()) return; + XSetLocaleModifiers(""); + dxc_im = XOpenIM(dxc_display, NULL, NULL, NULL); + if (dxc_im == NULL) return; + XIMStyles *styles = NULL; + if (XGetIMValues(dxc_im, XNQueryInputStyle, &styles, NULL) != NULL || styles == NULL) { + XCloseIM(dxc_im); dxc_im = NULL; return; + } + XIMStyle callbacks = XIMPreeditCallbacks | XIMStatusNothing; + XIMStyle plain = XIMPreeditNothing | XIMStatusNothing; + XIMStyle none = XIMPreeditNone | XIMStatusNone; + XIMStyle chosen = 0; + int have_callbacks = 0, have_plain = 0, have_none = 0; + for (int i = 0; i < styles->count_styles; i++) { + if (styles->supported_styles[i] == callbacks) have_callbacks = 1; + if (styles->supported_styles[i] == plain) have_plain = 1; + if (styles->supported_styles[i] == none) have_none = 1; + } + XFree(styles); + chosen = have_callbacks ? callbacks : have_plain ? plain : have_none ? none : 0; + if (chosen == 0) { XCloseIM(dxc_im); dxc_im = NULL; return; } + if (chosen == callbacks) { + dxc_cb_start.client_data = NULL; dxc_cb_start.callback = (XIMProc)dxc_preedit_start; + dxc_cb_draw.client_data = NULL; dxc_cb_draw.callback = (XIMProc)dxc_preedit_draw; + dxc_cb_done.client_data = NULL; dxc_cb_done.callback = (XIMProc)dxc_preedit_done; + dxc_cb_caret.client_data = NULL; dxc_cb_caret.callback = (XIMProc)dxc_preedit_caret; + XVaNestedList list = XVaCreateNestedList(0, + XNPreeditStartCallback, &dxc_cb_start, XNPreeditDrawCallback, &dxc_cb_draw, + XNPreeditDoneCallback, &dxc_cb_done, XNPreeditCaretCallback, &dxc_cb_caret, NULL); + dxc_ic = XCreateIC(dxc_im, XNInputStyle, chosen, XNClientWindow, dxc_window, + XNFocusWindow, dxc_window, XNPreeditAttributes, list, NULL); + XFree(list); + } else { + dxc_ic = XCreateIC(dxc_im, XNInputStyle, chosen, XNClientWindow, dxc_window, + XNFocusWindow, dxc_window, NULL); + } + if (dxc_ic == NULL) { XCloseIM(dxc_im); dxc_im = NULL; } +} + +/** Where the caret is, in pixels from the window's top left, for the candidate window. */ +void dxc_native_set_ime_spot(float x, float y) { + if (dxc_ic == NULL) return; + XPoint spot; + spot.x = (short)x; + spot.y = (short)y; + XVaNestedList list = XVaCreateNestedList(0, XNSpotLocation, &spot, NULL); + XSetICValues(dxc_ic, XNPreeditAttributes, list, NULL); + XFree(list); +} + +/** + * Ends what the input method is composing where it stands, and copies what it kept into + * [out] as UTF-8. Answers how many bytes that is, zero where there was nothing. + */ +int32_t dxc_native_ime_reset(char *out, int32_t capacity) { + if (dxc_ic == NULL || capacity <= 0) return 0; + char *kept = Xutf8ResetIC(dxc_ic); + if (kept == NULL) return 0; + size_t length = strlen(kept); + if (length > (size_t)capacity - 1) { + length = (size_t)capacity - 1; + while (length > 0 && ((unsigned char)kept[length] & 0xC0) == 0x80) length--; + } + memcpy(out, kept, length); + out[length] = 0; + XFree(kept); + return (int32_t)length; +} + +/* The bytes of a text property, or null. */ +static unsigned char *dxc_read_property(Window window, Atom property, unsigned long *length, + Atom *type) { + Atom actual = None; + int format = 0; + unsigned long count = 0, after = 0; + unsigned char *data = NULL; + if (XGetWindowProperty(dxc_display, window, property, 0, 1 << 20, True, AnyPropertyType, + &actual, &format, &count, &after, &data) != Success) { + return NULL; + } + *length = count * (unsigned long)(format / 8); + *type = actual; + return data; +} + +/** The clipboard's text as UTF-8 copied into [out], and its length; zero where none. */ +int32_t dxc_native_clipboard_read(char *out, int32_t capacity) { + if (dxc_display == NULL) return 0; + if (dxc_owns_clipboard && dxc_clip_text != NULL) { + if ((int32_t)dxc_clip_length > capacity) return 0; + memcpy(out, dxc_clip_text, dxc_clip_length); + return (int32_t)dxc_clip_length; + } + XConvertSelection(dxc_display, dxc_a_clipboard, dxc_a_utf8, dxc_a_property, dxc_window, + CurrentTime); + XFlush(dxc_display); + // The owner answers with an event, which this waits for without reading the others: + // they stay queued for the turn of the loop that handles them. + for (int attempt = 0; attempt < 100; attempt++) { + XEvent reply; + if (XCheckTypedWindowEvent(dxc_display, dxc_window, SelectionNotify, &reply)) { + if (reply.xselection.property == None) return 0; + unsigned long length = 0; + Atom type = None; + unsigned char *data = dxc_read_property(dxc_window, reply.xselection.property, + &length, &type); + if (data == NULL) return 0; + int32_t copied = 0; + if (type != dxc_a_incr && (int32_t)length <= capacity) { + memcpy(out, data, length); + copied = (int32_t)length; + } + XFree(data); + return copied; + } + struct timeval wait = {0, 5000}; + fd_set readable; + FD_ZERO(&readable); + FD_SET(ConnectionNumber(dxc_display), &readable); + select(ConnectionNumber(dxc_display) + 1, &readable, NULL, NULL, &wait); + } + return 0; +} + +/** Puts [text] on the clipboard and the primary selection, and answers for them. */ +void dxc_native_clipboard_write(const char *text) { + if (dxc_display == NULL || text == NULL) return; + free(dxc_clip_text); + dxc_clip_length = strlen(text); + dxc_clip_text = (char *)malloc(dxc_clip_length + 1); + if (dxc_clip_text == NULL) { dxc_clip_length = 0; return; } + memcpy(dxc_clip_text, text, dxc_clip_length + 1); + XSetSelectionOwner(dxc_display, dxc_a_clipboard, dxc_window, CurrentTime); + XSetSelectionOwner(dxc_display, dxc_a_primary, dxc_window, CurrentTime); + dxc_owns_clipboard = 1; + XFlush(dxc_display); +} + +/* Answers another program asking for what this window put on a selection. */ +static void dxc_answer_selection_request(XSelectionRequestEvent *request) { + XEvent reply; + memset(&reply, 0, sizeof reply); + reply.xselection.type = SelectionNotify; + reply.xselection.requestor = request->requestor; + reply.xselection.selection = request->selection; + reply.xselection.target = request->target; + reply.xselection.time = request->time; + reply.xselection.property = None; + Atom property = request->property != None ? request->property : request->target; + if (dxc_clip_text != NULL) { + if (request->target == dxc_a_targets) { + Atom targets[] = { dxc_a_targets, dxc_a_utf8, XA_STRING, dxc_a_text }; + XChangeProperty(dxc_display, request->requestor, property, XA_ATOM, 32, + PropModeReplace, (unsigned char *)targets, 4); + reply.xselection.property = property; + } else if (request->target == dxc_a_utf8 || request->target == XA_STRING || + request->target == dxc_a_text) { + XChangeProperty(dxc_display, request->requestor, property, + request->target == XA_STRING ? XA_STRING : dxc_a_utf8, 8, + PropModeReplace, (unsigned char *)dxc_clip_text, + (int)dxc_clip_length); + reply.xselection.property = property; + } + } + XSendEvent(dxc_display, request->requestor, False, NoEventMask, &reply); + XFlush(dxc_display); +} + +/** The paths of the files last dragged over the window, NUL between them, and the length. */ +int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { + if (dxc_dropped_length <= 0 || dxc_dropped_length > capacity) return 0; + memcpy(out, dxc_dropped_paths, (size_t)dxc_dropped_length); + return dxc_dropped_length; +} + +/* Turns the uri list a drag carries into the paths in it. */ +static void dxc_read_uri_list(const char *list, unsigned long length) { + int32_t used = 0; + unsigned long at = 0; + while (at < length) { + unsigned long end = at; + while (end < length && list[end] != '\r' && list[end] != '\n') end++; + if (end - at > 7 && strncmp(list + at, "file://", 7) == 0) { + unsigned long from = at + 7; + // A host name between the slashes is skipped; the path begins at the next one. + while (from < end && list[from] != '/') from++; + int32_t start = used; + if (used > 0) { + if (used + 1 >= DXC_DROPPED_BYTES) break; + dxc_dropped_paths[used++] = '\0'; + start = used; + } + for (unsigned long i = from; i < end && used < DXC_DROPPED_BYTES - 1; i++) { + char c = list[i]; + if (c == '%' && i + 2 < end) { + char hex[3] = { list[i + 1], list[i + 2], 0 }; + char *stop = NULL; + long value = strtol(hex, &stop, 16); + if (stop != NULL && *stop == '\0') { + dxc_dropped_paths[used++] = (char)value; + i += 2; + continue; + } + } + dxc_dropped_paths[used++] = c; + } + if (used == start) used = start > 0 ? start - 1 : 0; + } + at = end; + while (at < length && (list[at] == '\r' || list[at] == '\n')) at++; + } + dxc_dropped_length = used; +} + +static void dxc_push_drag(int32_t kind, int root_x, int root_y) { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = kind; + int x = 0, y = 0; + Window child; + XTranslateCoordinates(dxc_display, DefaultRootWindow(dxc_display), dxc_window, + root_x, root_y, &x, &y, &child); + record.x = (float)x; + record.y = (float)y; + dxc_push_event(record); +} + +static void dxc_send_dnd(Atom type, long a, long b, long c, long d) { + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_drag_source; + message.xclient.message_type = type; + message.xclient.format = 32; + message.xclient.data.l[0] = (long)dxc_window; + message.xclient.data.l[1] = a; + message.xclient.data.l[2] = b; + message.xclient.data.l[3] = c; + message.xclient.data.l[4] = d; + XSendEvent(dxc_display, dxc_drag_source, False, NoEventMask, &message); + XFlush(dxc_display); +} + +/* Handles the messages of the drag and drop protocol. Answers true where it was one. */ +static int dxc_handle_dnd(XClientMessageEvent *message) { + Atom type = message->message_type; + if (type == dxc_a_xdnd_enter) { + dxc_drag_source = (Window)message->data.l[0]; + int more = (message->data.l[1] & 1) != 0; + dxc_drag_has_files = 0; + if (!more) { + for (int i = 2; i < 5; i++) { + if ((Atom)message->data.l[i] == dxc_a_uri_list) dxc_drag_has_files = 1; + } + } else { + unsigned long length = 0; + Atom actual = None; + unsigned char *data = dxc_read_property(dxc_drag_source, dxc_a_xdnd_type_list, + &length, &actual); + if (data != NULL) { + // Read as the format 32 array it is: an array of long, whatever a long is. + long *types = (long *)data; + for (unsigned long i = 0; i < length / 4; i++) { + if ((Atom)types[i] == dxc_a_uri_list) dxc_drag_has_files = 1; + } + XFree(data); + } + } + return 1; + } + if (type == dxc_a_xdnd_position) { + int root_x = (int)((message->data.l[2] >> 16) & 0xFFFF); + int root_y = (int)(message->data.l[2] & 0xFFFF); + dxc_drag_root_x = root_x; + dxc_drag_root_y = root_y; + if (dxc_drag_has_files) dxc_push_drag(DXC_EVENT_FILES_ENTERED, root_x, root_y); + dxc_send_dnd(dxc_a_xdnd_status, dxc_drag_has_files ? 1 : 0, 0, 0, + dxc_drag_has_files ? (long)dxc_a_xdnd_copy : 0); + return 1; + } + if (type == dxc_a_xdnd_leave) { + if (dxc_drag_has_files) { + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_FILES_EXITED; + dxc_push_event(record); + } + dxc_drag_has_files = 0; + return 1; + } + if (type == dxc_a_xdnd_drop) { + dxc_drop_time = (Time)message->data.l[2]; + if (dxc_drag_has_files) { + XConvertSelection(dxc_display, dxc_a_xdnd_selection, dxc_a_uri_list, + dxc_a_property, dxc_window, dxc_drop_time); + } else { + dxc_send_dnd(dxc_a_xdnd_finished, 0, 0, 0, 0); + } + return 1; + } + return 0; +} + +/* The files arrived: take their paths, say where they were let go, and tell the source. */ +static void dxc_finish_drop(XSelectionEvent *arrived) { + int ok = 0; + if (arrived->property != None) { + unsigned long length = 0; + Atom type = None; + unsigned char *data = dxc_read_property(dxc_window, arrived->property, &length, &type); + if (data != NULL) { + dxc_read_uri_list((const char *)data, length); + XFree(data); + ok = dxc_dropped_length > 0; + } + } + if (ok) { + // Where the files were when they were let go, as the last position said, rather than + // where the pointer is by the time this is read. + dxc_push_drag(DXC_EVENT_FILES_DROPPED, dxc_drag_root_x, dxc_drag_root_y); + } + dxc_send_dnd(dxc_a_xdnd_finished, ok ? 1 : 0, ok ? (long)dxc_a_xdnd_copy : 0, 0, 0); + dxc_drag_has_files = 0; +} + +/** + * Does with the window what a button of the application's own caption asks: 0 minimises, + * 1 maximises or restores, 2 closes, 3 brings it forward. Safe from any thread for the + * last one only, which is a flag acted on in the next turn. + */ +void dxc_native_window_action(int32_t action) { + if (action == 3) { + dxc_raise_requested = 1; + return; + } + if (dxc_display == NULL || dxc_window == None) return; + switch (action) { + case 0: + XIconifyWindow(dxc_display, dxc_window, DefaultScreen(dxc_display)); + break; + case 1: { + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_window; + message.xclient.message_type = dxc_a_net_wm_state; + message.xclient.format = 32; + message.xclient.data.l[0] = 2; /* toggle */ + message.xclient.data.l[1] = (long)dxc_a_maximized_vert; + message.xclient.data.l[2] = (long)dxc_a_maximized_horz; + message.xclient.data.l[3] = 1; + XSendEvent(dxc_display, DefaultRootWindow(dxc_display), False, + SubstructureNotifyMask | SubstructureRedirectMask, &message); + break; + } + case 2: + dxc_closed = 1; + break; + default: + break; + } + XFlush(dxc_display); +} + +/** + * Hands the move or the resize to the window manager: 0 moves, and 1 to 8 pull the left, + * right, top and bottom edges and the four corners in that order. + */ +void dxc_native_window_begin_drag(int32_t edge) { + if (dxc_display == NULL || dxc_window == None) return; + // The directions _NET_WM_MOVERESIZE names, indexed by the edge numbers above. + static const long direction[9] = { 8, 7, 3, 1, 5, 0, 2, 6, 4 }; + if (edge < 0 || edge > 8) return; + Window root, child; + int root_x = 0, root_y = 0, x = 0, y = 0; + unsigned int mask = 0; + XQueryPointer(dxc_display, dxc_window, &root, &child, &root_x, &root_y, &x, &y, &mask); + XUngrabPointer(dxc_display, CurrentTime); + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_window; + message.xclient.message_type = dxc_a_moveresize; + message.xclient.format = 32; + message.xclient.data.l[0] = root_x; + message.xclient.data.l[1] = root_y; + message.xclient.data.l[2] = direction[edge]; + message.xclient.data.l[3] = Button1; + message.xclient.data.l[4] = 1; + XSendEvent(dxc_display, DefaultRootWindow(dxc_display), False, + SubstructureNotifyMask | SubstructureRedirectMask, &message); + XFlush(dxc_display); + // The manager owns the pointer from here, so the release that ends the press is never + // delivered to this window. The scene is told it happened or it would believe the + // button is still down. + struct dxc_event record; + memset(&record, 0, sizeof record); + record.kind = DXC_EVENT_POINTER_UP; + record.x = (float)x; + record.y = (float)y; + dxc_push_event(record); +} + +/** Reads how many pixels go to a point: the toolkit-independent settings desktops write. */ +static float dxc_detect_scale(void) { + const char *forced = getenv("GDK_SCALE"); + if (forced != NULL && atof(forced) >= 1.0) return (float)atof(forced); + char *resources = XResourceManagerString(dxc_display); + if (resources != NULL) { + XrmInitialize(); + XrmDatabase database = XrmGetStringDatabase(resources); + if (database != NULL) { + char *type = NULL; + XrmValue value; + if (XrmGetResource(database, "Xft.dpi", "Xft.Dpi", &type, &value) && value.addr != NULL) { + double dpi = atof(value.addr); + if (dpi >= 96.0) { + XrmDestroyDatabase(database); + return (float)(dpi / 96.0); + } + } + XrmDestroyDatabase(database); + } + } + return 1.0f; +} + static void dxc_pump_events(void) { if (dxc_pumping) { return; } dxc_pumping = 1; + if (dxc_raise_requested && dxc_display != NULL) { + dxc_raise_requested = 0; + XMapRaised(dxc_display, dxc_window); + XEvent message; + memset(&message, 0, sizeof message); + message.xclient.type = ClientMessage; + message.xclient.window = dxc_window; + message.xclient.message_type = dxc_a_active_window; + message.xclient.format = 32; + message.xclient.data.l[0] = 1; + XSendEvent(dxc_display, DefaultRootWindow(dxc_display), False, + SubstructureNotifyMask | SubstructureRedirectMask, &message); + XFlush(dxc_display); + } while (dxc_display != NULL && XPending(dxc_display) > 0) { XEvent event; XNextEvent(dxc_display, &event); + // Offered to the input method first. A key it takes is part of a composition and + // is not handled twice. + if (XFilterEvent(&event, None)) { + continue; + } struct dxc_event record; memset(&record, 0, sizeof record); switch (event.type) { @@ -234,7 +809,7 @@ static void dxc_pump_events(void) { record.x = (float)event.xmotion.x; record.y = (float)event.xmotion.y; record.buttons = dxc_buttons(event.xmotion.state); - record.modifiers = dxc_modifiers(event.xmotion.state); + record.modifiers = (int32_t)event.xmotion.state; break; case ButtonPress: case ButtonRelease: { @@ -253,22 +828,63 @@ static void dxc_pump_events(void) { if (event.type == ButtonPress) record.buttons |= bit; else record.buttons &= ~bit; } - record.modifiers = dxc_modifiers(event.xbutton.state); + record.modifiers = (int32_t)event.xbutton.state; break; } case KeyPress: case KeyRelease: { KeySym symbol = NoSymbol; - char bytes[32]; - int count = XLookupString(&event.xkey, bytes, sizeof bytes, &symbol, NULL); + char bytes[64]; + int count = 0; + Status status = XLookupNone; + if (event.type == KeyPress && dxc_ic != NULL) { + count = Xutf8LookupString(dxc_ic, &event.xkey, bytes, (int)sizeof bytes - 1, + &symbol, &status); + if (status == XBufferOverflow) count = 0; + } else { + count = XLookupString(&event.xkey, bytes, (int)sizeof bytes - 1, &symbol, NULL); + status = count > 0 ? XLookupChars : XLookupKeySym; + if (count > 0 && (unsigned char)bytes[0] >= 0x80) { + // Latin-1 from the plain lookup, re-encoded as the UTF-8 text is. + char converted[8]; + int length = dxc_encode_utf8((unsigned char)bytes[0], converted); + memcpy(bytes, converted, (size_t)length); + count = length; + } + } record.kind = event.type == KeyPress ? DXC_EVENT_KEY_DOWN : DXC_EVENT_KEY_UP; - record.key_code = dxc_key_code(symbol); - record.modifiers = dxc_modifiers(event.xkey.state); - if (count == 1 && (unsigned char)bytes[0] >= 32) { - record.code_point = (unsigned char)bytes[0]; + // The keysym, the state word and the text as the server gave them. What the + // key means, and whether its text is typed or only carried, is decided in + // `ImeComposition.kt` and `X11Keys.kt`, the same code the Kotlin/Native + // window asks. Text is sent for a press only. + record.key_code = (int32_t)symbol; + record.modifiers = (int32_t)event.xkey.state; + if (event.type == KeyPress && count > 0) { + size_t take = (size_t)count < DXC_TEXT_BYTES - 1 ? (size_t)count : DXC_TEXT_BYTES - 1; + while (take > 0 && take < (size_t)count && ((unsigned char)bytes[take] & 0xC0) == 0x80) { + take--; + } + memcpy(record.text, bytes, take); } break; } + case FocusIn: + if (dxc_ic != NULL) XSetICFocus(dxc_ic); + continue; + case FocusOut: + if (dxc_ic != NULL) XUnsetICFocus(dxc_ic); + continue; + case SelectionRequest: + dxc_answer_selection_request(&event.xselectionrequest); + continue; + case SelectionClear: + dxc_owns_clipboard = 0; + continue; + case SelectionNotify: + if (event.xselection.selection == dxc_a_xdnd_selection) { + dxc_finish_drop(&event.xselection); + } + continue; case ConfigureNotify: if (event.xconfigure.width == dxc_width && event.xconfigure.height == dxc_height) { // The window was moved, or told again what it already was. Nothing @@ -279,6 +895,7 @@ static void dxc_pump_events(void) { } dxc_width = event.xconfigure.width; dxc_height = event.xconfigure.height; + dxc_resize_step(&dxc_resize_stats, dxc_width, dxc_height); // The frame is drawn here, inside the handling of the size change, rather // than written down for the next turn of the loop. // @@ -304,6 +921,9 @@ static void dxc_pump_events(void) { } continue; case ClientMessage: + if (dxc_handle_dnd(&event.xclient)) { + continue; + } if (event.xclient.message_type == dxc_protocols && (Atom)event.xclient.data.l[0] == dxc_sync_request) { // The manager is about to resize the window and will hold the frame @@ -341,40 +961,25 @@ static void dxc_pump_events(void) { * ones that do not, which is a defect that travels to whoever builds for the strictest * platform. It travelled three times before this was written down. */ -int32_t dxc_native_clipboard_read(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; -} - -void dxc_native_clipboard_write(const char *text) { - (void)text; -} - void dxc_native_install_menu(const char *application_name) { (void)application_name; } /* - * Measuring aids of the AppKit window, named because the Kotlin that drives every desktop - * names them and answered here by doing nothing: a drag cannot be scripted from inside - * this window, and nothing asks for it to be. + * Measuring aids of the AppKit window that this window answers by doing nothing: a key + * cannot be posted from inside it, and nothing asks for one to be. The resize that can be + * scripted is `dxc_native_debug_resize`, below the pump it drives. */ -void dxc_native_debug_resize(void *window_pointer, void *view_pointer, int32_t from_width, - int32_t from_height, int32_t to_width, int32_t to_height, - int32_t steps, int32_t pause_micros) { - (void)window_pointer; (void)view_pointer; (void)from_width; (void)from_height; - (void)to_width; (void)to_height; (void)steps; (void)pause_micros; -} - void dxc_native_debug_key(void *window_pointer, int32_t key_code, const char *characters) { (void)window_pointer; (void)key_code; (void)characters; } -/* - * Asked for by name by the window code every desktop shares, and answered here by saying - * nothing: this window takes its options another way, has no strip of the system's to - * measure, and reads no list of dropped paths from the platform. +/** + * Says how the next window should be made. Called once, before it is opened. + * + * `system_chrome` keeps the window manager's frame. Without it the frame is taken off and + * the renderer draws the caption and the edges. `backdrop` means nothing here: this window + * has no material to put behind the page. */ void dxc_native_window_configure( int32_t resizable, @@ -383,11 +988,11 @@ void dxc_native_window_configure( int32_t system_chrome, int32_t backdrop ) { - (void)resizable; - (void)min_width; - (void)min_height; - (void)system_chrome; - (void)backdrop; + dxc_options.resizable = resizable; + dxc_options.min_width = min_width; + dxc_options.min_height = min_height; + dxc_options.system_chrome = system_chrome; + dxc_options.backdrop = backdrop; } void dxc_native_window_caption(void *view_pointer, float *height, float *buttons_width) { @@ -396,16 +1001,36 @@ void dxc_native_window_caption(void *view_pointer, float *height, float *buttons *buttons_width = 0; } +/** + * Gives the window the picture it named, from its pixels: eight bits each of red, green, + * blue and alpha, the colour already multiplied by the alpha, row after row. + */ void dxc_native_set_icon(const uint8_t *rgba, int32_t width, int32_t height) { - (void)rgba; - (void)width; - (void)height; -} - -int32_t dxc_native_dropped_paths(char *out, int32_t capacity) { - (void)out; - (void)capacity; - return 0; + if (dxc_display == NULL || dxc_window == None || rgba == NULL || width <= 0 || height <= 0) { + return; + } + size_t pixels = (size_t)width * (size_t)height; + // An array of long, whatever a long is: the width, the height, then one value a pixel + // with alpha in the top byte and the colour straight rather than multiplied. + unsigned long *data = (unsigned long *)malloc((pixels + 2) * sizeof *data); + if (data == NULL) return; + data[0] = (unsigned long)width; + data[1] = (unsigned long)height; + for (size_t index = 0; index < pixels; index++) { + unsigned long r = rgba[index * 4 + 0], g = rgba[index * 4 + 1]; + unsigned long b = rgba[index * 4 + 2], a = rgba[index * 4 + 3]; + if (a != 0 && a != 255) { + r = (r * 255 + a / 2) / a; g = (g * 255 + a / 2) / a; b = (b * 255 + a / 2) / a; + if (r > 255) r = 255; + if (g > 255) g = 255; + if (b > 255) b = 255; + } + data[index + 2] = (a << 24) | (r << 16) | (g << 8) | b; + } + XChangeProperty(dxc_display, dxc_window, dxc_a_net_wm_icon, XA_CARDINAL, 32, PropModeReplace, + (unsigned char *)data, (int)(pixels + 2)); + free(data); + XFlush(dxc_display); } void dxc_native_set_draw_callback(void *callback, void *isolate_thread) { @@ -465,6 +1090,11 @@ int32_t dxc_native_window_open(const char *title, int32_t width, int32_t height, dxc_display = XOpenDisplay(NULL); if (dxc_display == NULL) return 1; int screen = DefaultScreen(dxc_display); + // The size asked for is in points; the window is made in the pixels they come to on + // this display. + dxc_scale = dxc_detect_scale(); + width = (int32_t)((float)width * dxc_scale + 0.5f); + height = (int32_t)((float)height * dxc_scale + 0.5f); int attributes[] = { GLX_RGBA, GLX_DOUBLEBUFFER, GLX_RED_SIZE, 8, GLX_GREEN_SIZE, 8, GLX_BLUE_SIZE, 8, None }; XVisualInfo *visual = glXChooseVisual(dxc_display, screen, attributes); @@ -475,7 +1105,8 @@ int32_t dxc_native_window_open(const char *title, int32_t width, int32_t height, memset(&settings, 0, sizeof settings); settings.colormap = dxc_colormap; settings.event_mask = ExposureMask | StructureNotifyMask | PointerMotionMask | - ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask; + ButtonPressMask | ButtonReleaseMask | KeyPressMask | KeyReleaseMask | + FocusChangeMask; dxc_window = XCreateWindow(dxc_display, RootWindow(dxc_display, screen), 0, 0, (unsigned int)width, (unsigned int)height, 0, visual->depth, InputOutput, visual->visual, CWColormap | CWEventMask, &settings); @@ -490,6 +1121,7 @@ int32_t dxc_native_window_open(const char *title, int32_t width, int32_t height, dxc_display = NULL; return 3; } + if (getenv("DXC_REPORT_RESIZE") != NULL) atexit(dxc_resize_report); dxc_delete_window = XInternAtom(dxc_display, "WM_DELETE_WINDOW", False); dxc_protocols = XInternAtom(dxc_display, "WM_PROTOCOLS", False); dxc_sync_request = XInternAtom(dxc_display, "_NET_WM_SYNC_REQUEST", False); @@ -525,7 +1157,68 @@ int32_t dxc_native_window_open(const char *title, int32_t width, int32_t height, } } XSetWMProtocols(dxc_display, dxc_window, protocols, protocol_count); + dxc_a_clipboard = XInternAtom(dxc_display, "CLIPBOARD", False); + dxc_a_primary = XA_PRIMARY; + dxc_a_utf8 = XInternAtom(dxc_display, "UTF8_STRING", False); + dxc_a_targets = XInternAtom(dxc_display, "TARGETS", False); + dxc_a_text = XInternAtom(dxc_display, "TEXT", False); + dxc_a_incr = XInternAtom(dxc_display, "INCR", False); + dxc_a_property = XInternAtom(dxc_display, "DXC_SELECTION", False); + dxc_a_net_wm_name = XInternAtom(dxc_display, "_NET_WM_NAME", False); + dxc_a_net_wm_icon = XInternAtom(dxc_display, "_NET_WM_ICON", False); + dxc_a_net_wm_state = XInternAtom(dxc_display, "_NET_WM_STATE", False); + dxc_a_maximized_vert = XInternAtom(dxc_display, "_NET_WM_STATE_MAXIMIZED_VERT", False); + dxc_a_maximized_horz = XInternAtom(dxc_display, "_NET_WM_STATE_MAXIMIZED_HORZ", False); + dxc_a_moveresize = XInternAtom(dxc_display, "_NET_WM_MOVERESIZE", False); + dxc_a_active_window = XInternAtom(dxc_display, "_NET_ACTIVE_WINDOW", False); + dxc_a_motif_hints = XInternAtom(dxc_display, "_MOTIF_WM_HINTS", False); + dxc_a_xdnd_aware = XInternAtom(dxc_display, "XdndAware", False); + dxc_a_xdnd_enter = XInternAtom(dxc_display, "XdndEnter", False); + dxc_a_xdnd_position = XInternAtom(dxc_display, "XdndPosition", False); + dxc_a_xdnd_status = XInternAtom(dxc_display, "XdndStatus", False); + dxc_a_xdnd_leave = XInternAtom(dxc_display, "XdndLeave", False); + dxc_a_xdnd_drop = XInternAtom(dxc_display, "XdndDrop", False); + dxc_a_xdnd_finished = XInternAtom(dxc_display, "XdndFinished", False); + dxc_a_xdnd_selection = XInternAtom(dxc_display, "XdndSelection", False); + dxc_a_xdnd_copy = XInternAtom(dxc_display, "XdndActionCopy", False); + dxc_a_xdnd_type_list = XInternAtom(dxc_display, "XdndTypeList", False); + dxc_a_uri_list = XInternAtom(dxc_display, "text/uri-list", False); + XStoreName(dxc_display, dxc_window, title); + XChangeProperty(dxc_display, dxc_window, dxc_a_net_wm_name, dxc_a_utf8, 8, PropModeReplace, + (const unsigned char *)title, (int)strlen(title)); + // Sizes the window may take, in pixels. A window that cannot be resized is held to the + // size it opened at; otherwise only the minimum is stated. + { + XSizeHints hints; + memset(&hints, 0, sizeof hints); + if (dxc_options.min_width > 0 || dxc_options.min_height > 0) { + hints.flags |= PMinSize; + hints.min_width = (int)((float)dxc_options.min_width * dxc_scale + 0.5f); + hints.min_height = (int)((float)dxc_options.min_height * dxc_scale + 0.5f); + } + if (!dxc_options.resizable) { + hints.flags |= PMinSize | PMaxSize; + hints.min_width = hints.max_width = width; + hints.min_height = hints.max_height = height; + } + if (hints.flags != 0) XSetWMNormalHints(dxc_display, dxc_window, &hints); + } + if (!dxc_options.system_chrome) { + // No frame of the manager's: the renderer draws the caption and the edges. The + // fields are flags, functions, decorations, input mode and status; only the + // decorations are set, and to none. + unsigned long motif[5] = { 2, 0, 0, 0, 0 }; + XChangeProperty(dxc_display, dxc_window, dxc_a_motif_hints, dxc_a_motif_hints, 32, + PropModeReplace, (unsigned char *)motif, 5); + } + { + // This window takes files dropped on it, by version 5 of the protocol. + long version = 5; + XChangeProperty(dxc_display, dxc_window, dxc_a_xdnd_aware, XA_ATOM, 32, PropModeReplace, + (unsigned char *)&version, 1); + } + dxc_open_input_method(); XMapWindow(dxc_display, dxc_window); XFlush(dxc_display); dxc_width = width; @@ -549,7 +1242,7 @@ void dxc_native_window_size(void *window_pointer, int32_t *width, int32_t *heigh (void)window_pointer; *width = dxc_width; *height = dxc_height; - *scale = 1.0f; + *scale = dxc_scale; } int32_t dxc_native_frame_begin(void *window_pointer) { @@ -574,6 +1267,20 @@ void dxc_native_frame_end(void *display_pointer) { XFlush(dxc_display); } +/** + * Counts a frame handed to the screen, for the resize report: its picture was [width] by + * [height], and the window is asked how large it is now. Asked of the server rather than + * remembered, because what is being counted is a frame at a size the window no longer has. + * Does nothing unless DXC_REPORT_RESIZE is set, since the question costs a round trip. + */ +void dxc_native_note_present(int32_t width, int32_t height) { + if (dxc_display == NULL || dxc_closed || getenv("DXC_REPORT_RESIZE") == NULL) return; + XWindowAttributes attributes; + if (!XGetWindowAttributes(dxc_display, dxc_window, &attributes)) return; + dxc_resize_present_against(&dxc_resize_stats, width, height, attributes.width, + attributes.height); +} + /** * Lets the window answer for itself for a moment, and rests if it has nothing to say. * @@ -604,6 +1311,31 @@ void dxc_native_pump(double seconds) { dxc_pump_events(); } +/* + * Takes the window through the sizes a drag would, one after another, for measuring: a + * drag cannot be scripted without a hand, and what a drag does to the picture is the thing + * worth timing. Each size is asked of the server and the events it causes are read before + * the next, so the frame that belongs to a size is drawn inside the handling of it, as it + * is for a real drag. Used only when asked for, by DXC_SYNTH. + */ +void dxc_native_debug_resize(void *window_pointer, void *view_pointer, int32_t from_width, + int32_t from_height, int32_t to_width, int32_t to_height, + int32_t steps, int32_t pause_micros) { + (void)window_pointer; (void)view_pointer; + if (dxc_display == NULL || steps <= 0) return; + dxc_resize_reset(&dxc_resize_stats); + for (int32_t step = 1; step <= steps; step++) { + double t = (double)step / steps; + int32_t width = from_width + (int32_t)((to_width - from_width) * t); + int32_t height = from_height + (int32_t)((to_height - from_height) * t); + if (width < 1) width = 1; + if (height < 1) height = 1; + XResizeWindow(dxc_display, dxc_window, (unsigned)width, (unsigned)height); + XFlush(dxc_display); + dxc_native_pump(pause_micros / 1.0e6); + } +} + /** True once the reader has closed the window. */ int32_t dxc_native_window_closed(void) { return dxc_closed ? 1 : 0; diff --git a/renderer/desktop/src/AppKitKeys.kt b/renderer/desktop/src/AppKitKeys.kt index 84c2b14c3..7a796f20e 100644 --- a/renderer/desktop/src/AppKitKeys.kt +++ b/renderer/desktop/src/AppKitKeys.kt @@ -51,6 +51,17 @@ internal fun composeKey(platformKey: Int): Key = when (platformKey) { 0x28 -> Key.K 0x2D -> Key.N 0x2E -> Key.M + // The digits, for the same reason: a shortcut may be a number. + 0x12 -> Key.One + 0x13 -> Key.Two + 0x14 -> Key.Three + 0x15 -> Key.Four + 0x17 -> Key.Five + 0x16 -> Key.Six + 0x1A -> Key.Seven + 0x1C -> Key.Eight + 0x19 -> Key.Nine + 0x1D -> Key.Zero 0x24 -> Key.Enter 0x30 -> Key.Tab 0x31 -> Key.Spacebar diff --git a/renderer/desktop/src/AppKitWindow.kt b/renderer/desktop/src/AppKitWindow.kt index f06eace8f..5f9a5f0d2 100644 --- a/renderer/desktop/src/AppKitWindow.kt +++ b/renderer/desktop/src/AppKitWindow.kt @@ -137,6 +137,18 @@ private external fun setIcon(rgba: CCharPointer?, width: Int, height: Int) @CFunction("dxc_native_dropped_paths") private external fun droppedPaths(out: Pointer?, capacity: Int): Int +@CFunction("dxc_native_set_ime_spot") +private external fun setImeSpot(x: Float, y: Float) + +@CFunction("dxc_native_ime_reset") +private external fun resetInputContext(out: Pointer?, capacity: Int): Int + +@CFunction("dxc_native_window_action") +private external fun windowAction(action: Int) + +@CFunction("dxc_native_window_begin_drag") +private external fun beginWindowDrag(edge: Int) + @CFunction("dxc_native_window_closed") private external fun windowClosed(): Int @@ -324,6 +336,33 @@ internal fun NativeWindow.measureCaption(): dev.darkpyonix.composerust.runtime.W ) } +/** Tells the window where the caret is, so the input method's candidates open beside it. */ +internal fun reportCaret(textInput: NativeTextInput) { + val spot = textInput.caretSpot() ?: return + if (spot == lastCaret) return + lastCaret = spot + setImeSpot(spot.x, spot.y) +} + +private var lastCaret: androidx.compose.ui.geometry.Offset? = null + +/** Brings the window forward. Safe from any thread: it is a request the window acts on in its next turn. */ +internal fun bringNativeWindowToFront() = windowAction(3) + +/** What a button of the application's own caption does to the window. */ +internal fun nativeWindowActions() = dev.darkpyonix.composerust.runtime.WindowActions( + minimise = { windowAction(0) }, + maximise = { windowAction(1) }, + close = { windowAction(2) }, +) + +/** + * Hands the move or the resize of an undecorated window to the window manager, which does + * it better than a drag measured here could: 0 moves, and 1 to 8 pull the edge or corner + * [WindowEdge] names. + */ +internal fun beginNativeWindowDrag(edge: Int) = beginWindowDrag(edge) + /** The paths of the files last dragged over the window, one string, NUL between them. */ internal fun readDroppedPaths(): String { val buffer = StackValue.get(DROPPED_PATHS_BYTES) @@ -353,6 +392,22 @@ internal fun setApplicationIcon(rgba: ByteArray, width: Int, height: Int) { } } +/** + * Puts the picture the application named on the window, once the asset has arrived. + * + * Asked each frame until it is there, because the id is known from the first batch and the + * bitmap a little later. Answers true once it has been put on, so the caller can stop. + */ +internal fun applyNamedIcon(host: dev.darkpyonix.composerust.runtime.ComposeRustHost, id: Int): Boolean { + if (id == 0) return true + val raster = host.table.assets.asset(id) as? dev.darkpyonix.composerust.ui.node.Asset.Raster + ?: return false + iconPixels(raster.bitmap)?.let { (pixels, width, height) -> + setApplicationIcon(pixels, width, height) + } + return true +} + /** The pixels [setApplicationIcon] takes, from a picture Compose holds. */ internal fun iconPixels(picture: androidx.compose.ui.graphics.ImageBitmap): Triple? { val bitmap = picture.asSkiaBitmap() @@ -394,6 +449,23 @@ fun installApplicationMenu(name: String) { } } +/** + * Ends what the input method is composing and answers the text it kept, or empty where it kept + * none. Only the X11 window has a composition of its own to end. + */ +internal fun resetInputMethod(): String { + val buffer = StackValue.get(RESET_BYTES) + val length = resetInputContext(buffer, RESET_BYTES) + if (length <= 0) return "" + val bytes = ByteArray(length) + for (index in 0 until length) { + bytes[index] = buffer.readByte(index) + } + return String(bytes, Charsets.UTF_8) +} + +private const val RESET_BYTES = 256 + /** What is on the clipboard, or empty where it holds something that is not text. */ fun readClipboard(): String { val buffer = StackValue.get(CLIPBOARD_BYTES) @@ -492,17 +564,9 @@ private const val WINDOW_STRUCT_BYTES = 40 * watches. */ internal fun runAppKitWindow(autoExitMillis: Long? = null) { - // Before the first frame, because every piece of text drawn after this reads them. - dev.darkpyonix.composerust.design.installPlatformUiFamily() - dev.darkpyonix.composerust.ui.installReducedMotion() - dev.darkpyonix.composerust.ui.installHighContrast() - dev.darkpyonix.composerust.ui.node.platformFileDrop = { modifier, node, dispatcher -> - modifier.nativeFileDrop(node, dispatcher) - } // The window is a real one with the desktop behind it, so a design that draws glass - // can let that show through. Set before the Host starts: its first batch may already - // ask. - dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { true } + // can let that show through. + installNativeWindowHooks(backdropSupported = true) // The Host is started before there is a window, because what the window should look // like is in its first batch and a window cannot be told afterwards. Started on this // thread, which is the one every later call to it is made from: the boundary is a @@ -510,19 +574,16 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window - val backdrop = host.table.asksForWindowMaterial(host.roots) + val options = nativeWindowOptions(host, backdropSupported = true) + val backdrop = options.backdrop configureNativeWindow( - resizable = asked?.resizable ?: true, - minWidth = asked?.minWidth?.takeIf { it > 0 } ?: 0, - minHeight = asked?.minHeight?.takeIf { it > 0 } ?: 0, - systemChrome = asked?.chrome == dev.darkpyonix.composerust.protocol.Chrome.System, + resizable = options.resizable, + minWidth = options.minWidth, + minHeight = options.minHeight, + systemChrome = options.systemChrome, backdrop = backdrop, ) - val window = openNativeWindow( - asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", - if (asked != null && asked.width > 0) asked.width else 520, - if (asked != null && asked.height > 0) asked.height else 360, - ) + val window = openNativeWindow(options.title, options.width, options.height) if (window == null) { System.err.println("compose-rust: this machine has no Metal device") return @@ -565,25 +626,10 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { ) // The application's own tree, drawn by the same interpreter every window uses. // Nothing in it knows which window it is in, which is the point. - val clipboard = WindowClipboard() - @Suppress("DEPRECATION") - val clipboardManager = WindowClipboardManager() scene.setContent { - androidx.compose.runtime.CompositionLocalProvider( - dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver provides { - rememberSystemDark().value - }, - androidx.compose.ui.platform.LocalClipboard provides clipboard, - androidx.compose.ui.platform.LocalClipboardManager provides clipboardManager, - ) { - dev.darkpyonix.composerust.runtime.ComposeRustContent( - host, - Modifier.fillMaxSize(), - caption = caption.value, - ) - } + NativeWindowContent(host, caption.value, actions = null) } - installApplicationMenu(asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust") + installApplicationMenu(options.title) val started = System.nanoTime() var iconId = 0 @@ -619,6 +665,7 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { var heard = false val events = drainWindowEvents() for (event in synthetic?.due(System.nanoTime(), size) ?: emptyList()) { + if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() scene.receive(event) textInput.receive(event) LatencyTrace.mark("synthetic ${event.kind} sent") @@ -684,8 +731,10 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { pumpWindowEvents(FRAME_SECONDS) LatencyTrace.mark("loop turn $frame") if (synthetic != null && synthetic.resizeDue(System.nanoTime())) { + LatencyTrace.phase = "resize" window.scriptedResize(size.width / measured.scale.toInt() to size.height / measured.scale.toInt(), 360 to 420, 60, 8_000) + LatencyTrace.phase = "idle" } synthetic?.keysDue(System.nanoTime())?.let { (code, character) -> LatencyTrace.mark("synthetic key '$character' posted") @@ -698,6 +747,7 @@ internal fun runAppKitWindow(autoExitMillis: Long? = null) { // a window that has gone still is exactly where that would be forgotten. // Costs a comparison when nothing has changed, which is almost always. semantics.pushIfChanged(afterDrawing = drew) + reportCaret(textInput) } LatencyTrace.summary() } finally { diff --git a/renderer/desktop/src/ImeComposition.kt b/renderer/desktop/src/ImeComposition.kt new file mode 100644 index 000000000..0d4ec73ec --- /dev/null +++ b/renderer/desktop/src/ImeComposition.kt @@ -0,0 +1,179 @@ +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.ui.geometry.Rect + +// What an input method says, turned into what the shared reader hears. +// +// One model for both X11 windows. The window that links the native image gets the input +// method's callbacks in C and the Kotlin/Native window gets them from Xlib; each hands what +// it was told to this file as plain numbers and text, so a preedit edit applied at the +// wrong offset, or a commit dropped when a syllable is finished, can be wrong in one place +// only and is checked by tests that need no input method daemon. +// +// Nothing here reaches Rust. What an input method produces is a `WindowEvent` of the kinds +// the window already has, recorded into the same log as a pointer move, and it ends in the +// text input session Compose opened for the focused field. + +/** + * The text an input method is still composing, kept the way X's preedit callbacks describe + * it: as edits to a run of characters. + * + * The draw callback does not send the whole string. It says "replace [length] characters + * from [first] with this", so a client has to hold the run to apply that to. Counted in code + * points and not in UTF-16 units, because that is what the protocol counts: a Hangul syllable + * is one, and so is a character outside the Basic Multilingual Plane, which is two units. + */ +internal class PreeditBuffer { + private val points = ArrayList() + + /** Where the input method says the caret is inside the run, in characters. */ + var caret = 0 + private set + + val isEmpty: Boolean get() = points.isEmpty() + + /** The run as a string, in the encoding Compose reads. */ + val text: String + get() = buildString { for (point in points) appendPoint(point) } + + /** + * Applies one draw callback. + * + * A start past the end is clamped to the end rather than rejected: a client that threw + * here would be a window that dies in the middle of someone's sentence, and appending is + * what an input method that miscounted meant. + */ + fun replace(first: Int, length: Int, with: String, newCaret: Int) { + val start = first.coerceIn(0, points.size) + val end = (start + length.coerceAtLeast(0)).coerceAtMost(points.size) + for (index in end - 1 downTo start) points.removeAt(index) + points.addAll(start, codePointsOf(with)) + caret = newCaret.coerceIn(0, points.size) + } + + fun clear() { + points.clear() + caret = 0 + } +} + +/** + * One client's side of an input method: the callbacks it makes, and what each of them means. + * + * Takes [emit] rather than a window, so that a test hands it a list and a window hands it the + * log every other event goes into. Everything arrives on the thread that reads the display + * server, from inside the one call that reads it, so nothing here is ever re-entered and + * nothing needs a lock. + */ +internal class ImeSession(private val emit: (WindowEvent) -> Unit) { + private val preedit = PreeditBuffer() + + /** Whether something is being composed, which is what a click has to finish. */ + val composing: Boolean get() = !preedit.isEmpty + + /** The input method begins a composition. Nothing is shown until it draws. */ + fun preeditStart() { + preedit.clear() + } + + /** + * The composition changed. Compose is given the whole run each time, because that is what + * its command replaces: the marked text is the run, and what was marked before is gone. + */ + fun preeditDraw(first: Int, length: Int, text: String, caret: Int) { + preedit.replace(first, length, text, caret) + emit(imeEvent(WindowEvent.TEXT_COMPOSE, preedit.text)) + } + + /** The composition ended without committing anything: the reader backed out of it. */ + fun preeditDone() { + if (preedit.isEmpty) return + preedit.clear() + emit(imeEvent(WindowEvent.TEXT_COMPOSE, "")) + } + + /** + * Finished text. Whatever was being composed is taken out first: some input methods clear + * the preedit and then commit, and some commit and never clear it, and a field must not be + * left holding both the syllable and the mark that was over it. + */ + fun commit(text: String) { + if (text.isEmpty()) return + preeditDone() + emit(imeEvent(WindowEvent.TEXT_COMMIT, text)) + } +} + +/** A record for something an input method said: no position, no key, only the text. */ +internal fun imeEvent(kind: Int, text: String) = WindowEvent( + kind = kind, + x = 0f, + y = 0f, + buttons = 0, + modifiers = 0, + keyCode = 0, + codePoint = 0, + text = text, +) + +/** + * What one key press or release becomes, given what the keyboard and the input method said it + * typed. + * + * [text] is whatever came back from asking for the characters of the event: empty for a key + * that types nothing, one character for most, and several for an input method that committed + * a word. Three cases follow from it, and each exists because of a way this went wrong. + * + * - A key that types one plain ASCII character keeps carrying it in the key event, which is + * how every field here has been typed into so far. Moving it would change what a shortcut + * sees. + * - Anything else printable is committed as text and the key event carries no character. A + * Hangul syllable put in a key event would be read as one code unit by a field that expects + * a character from a keyboard. + * - An event with no key behind it at all, which is what an input method sends to say it has + * committed, produces the commit and no key event: there is nothing for a shortcut to + * see, and a key with no name pressed in a text field would only confuse a handler. + */ +internal fun keyEventsFor( + press: Boolean, + state: Int, + keysym: Long, + text: String, +): List { + val key = x11KeyNumber(keysym) + val modifiers = x11Modifiers(state) + if (!press) { + return listOf( + WindowEvent(WindowEvent.KEY_UP, 0f, 0f, 0, modifiers, key, 0, ""), + ) + } + val first = text.firstOrNull() + val printable = first != null && first.code >= FIRST_PRINTABLE && first.code != DELETE_CHARACTER + val plain = text.length == 1 && first!!.code in FIRST_PRINTABLE until DELETE_CHARACTER + val shortcut = x11IsShortcut(state) + val down = WindowEvent( + WindowEvent.KEY_DOWN, 0f, 0f, 0, modifiers, key, + if (plain) first!!.code else 0, "", + ) + if (!printable || plain) return listOf(down) + if (shortcut) return listOf(down) + val commit = imeEvent(WindowEvent.TEXT_COMMIT, text) + return if (keysym == 0L) listOf(commit) else listOf(down, commit) +} + +/** + * Where an input method should put its candidate window: under the caret, in the window's own + * pixels, or null where the field has not been laid out yet. + * + * The rectangle is Compose's own answer for the focused field's caret, in the root's units. + * The scene's density is one point to a pixel (see the window's note on it), so no scale + * is applied; [density] is there so that changing that changes one place. + */ +internal fun candidateSpot(caret: Rect?, density: Float): Pair? { + if (caret == null || caret.isEmpty) return null + return (caret.left * density).toInt() to (caret.bottom * density).toInt() +} + +private const val FIRST_PRINTABLE = 32 +private const val DELETE_CHARACTER = 0x7F + diff --git a/renderer/desktop/src/LatencyTrace.kt b/renderer/desktop/src/LatencyTrace.kt index 2e692e586..b09a1030d 100644 --- a/renderer/desktop/src/LatencyTrace.kt +++ b/renderer/desktop/src/LatencyTrace.kt @@ -1,13 +1,23 @@ package dev.darkpyonix.composerust.ui.platform +import java.lang.System +import kotlin.math.roundToLong +import org.jetbrains.skiko.currentSystemTheme + /** - * Timestamps along the path from a key press to the pixels, on request. + * Timestamps along the path from a key press to the pixels, on request, and the numbers the + * parity check reads. * * Off unless `DXC_REPORT_LATENCY` is set, because it prints a line per step. Each line * carries milliseconds on the monotonic clock, so two lines can be subtracted, and the * label says which step was reached: the window heard the key, the field changed, the Host * was called, it asked for a frame, the frame was rendered, the batch applied, the pixels * drawn. + * + * Shared by every window that draws a scene, so the same run on any of them ends in the same + * lines. [summary] prints them: one `parity` line per measurement, `parity + * ` with the path left to whoever reads the log, because the window does not know which + * of the two builds it is in and a log is read next to the command that made it. */ internal object LatencyTrace { val enabled: Boolean = System.getenv("DXC_REPORT_LATENCY") != null @@ -15,29 +25,84 @@ internal object LatencyTrace { fun mark(label: String) { if (enabled) { val millis = System.nanoTime() / 1_000.0 / 1_000.0 - System.err.println("compose-rust: latency t=${"%.3f".format(millis)} $label") + System.err.println("compose-rust: latency t=${fixed(millis, 3)} $label") } } - private var frames = 0 - private var totalNanos = 0L - private var worstNanos = 0L + /** What is being measured now, so a drag of the window and a quiet window are counted apart. */ + var phase: String = "idle" + + private class Frames { + var count = 0 + var total = 0L + var worst = 0L + } + + private val byPhase = LinkedHashMap() + private var inputSentAt = 0L + private var inputSamples = 0 + private var inputTotal = 0L + private var inputWorst = 0L /** Counts one drawn frame and how long it took, for [summary]. */ fun frameDrawn(nanos: Long) { if (!enabled) return - frames++ - totalNanos += nanos - if (nanos > worstNanos) worstNanos = nanos + val frames = byPhase.getOrPut(phase) { Frames() } + frames.count++ + frames.total += nanos + if (nanos > frames.worst) frames.worst = nanos + if (inputSentAt != 0L) { + // From the input being handed to the scene to the frame it changed being drawn. + val spent = System.nanoTime() - inputSentAt + inputSentAt = 0L + inputSamples++ + inputTotal += spent + if (spent > inputWorst) inputWorst = spent + } + } + + /** Notes that a typed letter was just handed to the scene. The next frame drawn answers it. */ + fun inputSent() { + if (enabled && inputSentAt == 0L) inputSentAt = System.nanoTime() } - /** One line at the end of a run: how many frames were drawn and what each cost. */ + /** The lines at the end of a run: what each frame cost, per phase, and what an input cost. */ fun summary() { - if (enabled && frames > 0) { + if (!enabled) return + System.err.println("compose-rust: parity system_theme ${currentSystemTheme.name.lowercase()}") + var frames = 0 + var total = 0L + var worst = 0L + for ((_, one) in byPhase) { + frames += one.count + total += one.total + if (one.worst > worst) worst = one.worst + } + if (frames > 0) { System.err.println( - "compose-rust: latency frames=$frames average=${"%.2f".format(totalNanos / frames / 1.0e6)}ms " + - "worst=${"%.2f".format(worstNanos / 1.0e6)}ms", + "compose-rust: latency frames=$frames average=${fixed(total / frames / 1.0e6, 2)}ms " + + "worst=${fixed(worst / 1.0e6, 2)}ms", + ) + } + for ((name, one) in byPhase) { + System.err.println( + "compose-rust: parity frame_${name}_avg_ms ${fixed(one.total / one.count / 1.0e6, 2)} " + + "frames=${one.count} worst_ms=${fixed(one.worst / 1.0e6, 2)}", + ) + } + if (inputSamples > 0) { + System.err.println( + "compose-rust: parity input_latency_avg_ms ${fixed(inputTotal / inputSamples / 1.0e6, 2)} " + + "samples=$inputSamples worst_ms=${fixed(inputWorst / 1.0e6, 2)}", ) } } } + +/** [value] with [digits] places after the point, which Kotlin/Native has no `format` for. */ +internal fun fixed(value: Double, digits: Int): String { + var factor = 1L + repeat(digits) { factor *= 10 } + val scaled = (value.coerceAtLeast(0.0) * factor).roundToLong() + return "${scaled / factor}.${(scaled % factor).toString().padStart(digits, '0')}" +} diff --git a/renderer/desktop/src/NativeClipboard.kt b/renderer/desktop/src/NativeClipboard.kt index b415a88b9..0802d411d 100644 --- a/renderer/desktop/src/NativeClipboard.kt +++ b/renderer/desktop/src/NativeClipboard.kt @@ -33,7 +33,7 @@ internal class WindowClipboardManager : ClipboardManager { override fun hasText(): Boolean = clipboardText() != null } -internal class WindowClipboard : Clipboard { +internal class WindowClipboardImpl : Clipboard { override suspend fun getClipEntry(): ClipEntry? = clipboardText()?.let { ClipEntry(StringSelection(it)) } @@ -46,3 +46,9 @@ internal class WindowClipboard : Clipboard { override val nativeClipboard: NativeClipboard get() = this } + +/** One of each for every window: they hold nothing, they only ask the platform. */ +internal val WindowClipboard: Clipboard = WindowClipboardImpl() + +@Suppress("DEPRECATION") +internal val WindowClipboardManagerInstance: ClipboardManager = WindowClipboardManager() diff --git a/renderer/desktop/src/NativeWindowOptions.kt b/renderer/desktop/src/NativeWindowOptions.kt new file mode 100644 index 000000000..9a0f46d31 --- /dev/null +++ b/renderer/desktop/src/NativeWindowOptions.kt @@ -0,0 +1,45 @@ +package dev.darkpyonix.composerust.ui.platform + +import dev.darkpyonix.composerust.protocol.Chrome +import dev.darkpyonix.composerust.runtime.ComposeRustHost +import dev.darkpyonix.composerust.runtime.asksForWindowMaterial + +// What the application asked of its window, read the same way by every window of our own: +// the native image windows and the Kotlin/Native ones. One reading, so that a title, a size or +// a smallest size means the same thing on every path. + +/** What the application asked of its window in its first batch. */ +internal data class NativeWindowOptions( + val title: String, + val width: Int, + val height: Int, + val resizable: Boolean, + val minWidth: Int, + val minHeight: Int, + val systemChrome: Boolean, + val backdrop: Boolean, +) + +/** + * Reads the window's options from the Host's first batch. + * + * [backdropSupported] says whether this platform's window can show what is behind it, + * because asking for a material the window cannot draw would leave a design drawing for a + * desktop that never shows through. + */ +internal fun nativeWindowOptions(host: ComposeRustHost, backdropSupported: Boolean): NativeWindowOptions { + val asked = host.table.window + return NativeWindowOptions( + title = asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", + width = if (asked != null && asked.width > 0) asked.width else DEFAULT_WIDTH, + height = if (asked != null && asked.height > 0) asked.height else DEFAULT_HEIGHT, + resizable = asked?.resizable ?: true, + minWidth = asked?.minWidth?.takeIf { it > 0 } ?: 0, + minHeight = asked?.minHeight?.takeIf { it > 0 } ?: 0, + systemChrome = asked?.chrome == Chrome.System, + backdrop = backdropSupported && host.table.asksForWindowMaterial(host.roots), + ) +} + +private const val DEFAULT_WIDTH = 520 +private const val DEFAULT_HEIGHT = 360 diff --git a/renderer/desktop/src/NativeWindowShell.kt b/renderer/desktop/src/NativeWindowShell.kt new file mode 100644 index 000000000..3a1575f20 --- /dev/null +++ b/renderer/desktop/src/NativeWindowShell.kt @@ -0,0 +1,143 @@ +package dev.darkpyonix.composerust.ui.platform + +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxScope +import androidx.compose.foundation.gestures.awaitEachGesture +import androidx.compose.foundation.gestures.awaitFirstDown +import androidx.compose.foundation.layout.fillMaxHeight +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.height +import androidx.compose.foundation.layout.size +import androidx.compose.foundation.layout.width +import androidx.compose.ui.Alignment +import androidx.compose.ui.input.pointer.pointerInput +import androidx.compose.ui.unit.dp +import androidx.compose.runtime.Composable +import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.ui.Modifier +import androidx.compose.ui.platform.LocalClipboard +import androidx.compose.ui.platform.LocalClipboardManager +import dev.darkpyonix.composerust.runtime.ComposeRustContent +import dev.darkpyonix.composerust.runtime.ComposeRustHost +import dev.darkpyonix.composerust.runtime.LocalSystemDarkObserver +import dev.darkpyonix.composerust.runtime.LocalWindowActions +import dev.darkpyonix.composerust.runtime.WindowActions +import dev.darkpyonix.composerust.runtime.WindowCaption + +// What every window of our own has in common, so that each platform's file is only what is +// different about its window: the things the application asked of it, the hooks that make +// the tree behave as it does on a desktop, and the content that goes inside. + +/** + * Installs what the tree reads from the platform, before the Host starts: its first batch + * may already ask. + */ +internal fun installNativeWindowHooks(backdropSupported: Boolean) { + dev.darkpyonix.composerust.design.installPlatformUiFamily() + dev.darkpyonix.composerust.ui.installReducedMotion() + dev.darkpyonix.composerust.ui.installHighContrast() + dev.darkpyonix.composerust.ui.installResizeCursor() + dev.darkpyonix.composerust.ui.node.platformFileDrop = { modifier, node, dispatcher -> + modifier.nativeFileDrop(node, dispatcher) + } + dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { backdropSupported } +} + +/** + * The application's tree inside a window of our own. + * + * [overlay] is drawn last, over the content: the resize edges of a window with no frame of + * the system's. [drag] is drawn first, under it, so a widget in the caption takes the press + * and the strip only sees the room around it. + */ +@Suppress("DEPRECATION") +@Composable +internal fun NativeWindowContent( + host: ComposeRustHost, + caption: WindowCaption, + actions: WindowActions?, + drag: @Composable BoxScope.() -> Unit = {}, + overlay: @Composable BoxScope.() -> Unit = {}, +) { + CompositionLocalProvider( + LocalSystemDarkObserver provides { rememberSystemDark().value }, + LocalClipboard provides WindowClipboard, + LocalClipboardManager provides WindowClipboardManagerInstance, + LocalWindowActions provides actions, + ) { + Box(Modifier.fillMaxSize()) { + drag() + ComposeRustContent(host, Modifier.fillMaxSize(), caption = caption) + overlay() + } + } +} + +/** + * The strip across the top of a window with no frame of the system's that moves it when + * pressed, and maximises it when pressed twice. + * + * Drawn under the content, so a widget in the caption takes the press and the strip only + * sees the room around it. The move itself is the window manager's, handed over at the + * press: a drag measured here would lag it. + */ +@Composable +internal fun NativeCaptionDrag(height: androidx.compose.ui.unit.Dp, actions: WindowActions?) { + val lastPress = androidx.compose.runtime.remember { LongArray(1) } + Box( + Modifier + .fillMaxWidth() + .height(height) + .pointerInput(Unit) { + awaitEachGesture { + awaitFirstDown() + val now = System.nanoTime() + if (now - lastPress[0] < DOUBLE_PRESS_NANOS) { + lastPress[0] = 0L + actions?.maximise?.invoke() + } else { + lastPress[0] = now + beginNativeWindowDrag(0) + } + } + }, + ) +} + +private const val DOUBLE_PRESS_NANOS = 400_000_000L + +/** How wide the invisible strip along each edge of a window with no frame is. */ +private val EDGE_GRIP = 6.dp + +/** + * The eight edges of a window with no frame of the system's, as things to press. + * + * Drawn last so they sit over the content, and as wide as a pointer rather than as anything + * visible. The resize is the window manager's, handed over at the press. + */ +@Composable +internal fun BoxScope.NativeResizeEdges() { + // The numbers are the ones beginNativeWindowDrag takes: left, right, top, bottom, then + // the four corners. + Edge(1, Modifier.fillMaxHeight().width(EDGE_GRIP), Alignment.CenterStart) + Edge(2, Modifier.fillMaxHeight().width(EDGE_GRIP), Alignment.CenterEnd) + Edge(3, Modifier.fillMaxWidth().height(EDGE_GRIP), Alignment.TopCenter) + Edge(4, Modifier.fillMaxWidth().height(EDGE_GRIP), Alignment.BottomCenter) + Edge(5, Modifier.size(EDGE_GRIP), Alignment.TopStart) + Edge(6, Modifier.size(EDGE_GRIP), Alignment.TopEnd) + Edge(7, Modifier.size(EDGE_GRIP), Alignment.BottomStart) + Edge(8, Modifier.size(EDGE_GRIP), Alignment.BottomEnd) +} + +@Composable +private fun BoxScope.Edge(edge: Int, size: Modifier, alignment: Alignment) { + Box( + Modifier.align(alignment).then(size).pointerInput(edge) { + awaitEachGesture { + awaitFirstDown() + beginNativeWindowDrag(edge) + } + }, + ) +} diff --git a/renderer/desktop/src/RendererEntryPoints.kt b/renderer/desktop/src/RendererEntryPoints.kt index fe99768ba..cf48aa87f 100644 --- a/renderer/desktop/src/RendererEntryPoints.kt +++ b/renderer/desktop/src/RendererEntryPoints.kt @@ -5,12 +5,13 @@ package dev.darkpyonix.composerust.ui.platform import org.graalvm.nativeimage.IsolateThread import org.graalvm.nativeimage.c.function.CEntryPoint import org.graalvm.nativeimage.c.type.CCharPointer +import org.graalvm.nativeimage.c.function.CFunction import org.graalvm.nativeimage.c.type.CTypeConversion import dev.darkpyonix.composerust.ui.platform.NativeHostConnection +import dev.darkpyonix.composerust.ui.platform.bringNativeWindowToFront import dev.darkpyonix.composerust.ui.platform.runAppKitWindow import dev.darkpyonix.composerust.ui.platform.runWin32Window import dev.darkpyonix.composerust.ui.platform.runX11Window -import org.graalvm.nativeimage.c.function.CFunction // C entry points of the renderer shared library. // @@ -37,12 +38,16 @@ private external fun setWindowMaterial(asked: Int) fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = try { configureRuntimeLayout(CTypeConversion.toJavaString(libraryDir)) - // Which window this platform opens. macOS opens one of its own, made from AppKit - // and Metal with no toolkit between; the other platforms still open the - // toolkit's. The C files behind each answer for the same symbols, so the one - // compiled into an image is the one that can be reached: which platform this is - // decides, and nothing is read from the environment. + // Which window this platform opens. Each desktop opens one of its own, made from + // the platform's windowing and graphics APIs with no toolkit between: AppKit and + // Metal, Win32 and Direct3D 12, X11 and GLX. The C files behind each answer for the + // same symbols, so the one compiled into an image is the one that can be reached: + // which platform this is decides, and nothing is read from the environment. val platform = System.getProperty("os.name", "") + // Linux still opens the toolkit's window unless asked for the X11 one: that window + // is finished but its typing, clipboard and input method have not been checked on a + // real desktop, so it is not the default yet. + val linuxOwnWindow = platform.startsWith("Linux") && System.getenv("DXC_X11_WINDOW") != null val autoExitMillis = (System.getenv("COMPOSE_RUST_AUTOEXIT_MS") ?: System.getenv("DIOXUS_COMPOSE_AUTOEXIT_MS")) ?.toLongOrNull() @@ -56,38 +61,37 @@ fun rendererRun(thread: IsolateThread?, libraryDir: CCharPointer?): Int = NativeDesktopNotifications() platform.startsWith("Linux") -> DBusNotifications( open = { JvmBusConnection.open(wake = FrameRequests::request) }, - bringToFront = ::bringAwtWindowToFront, + bringToFront = if (linuxOwnWindow) ::bringNativeWindowToFront else ::bringAwtWindowToFront, applicationName = JvmBusConnection.applicationName(), ) else -> UnsupportedNotifications } - if (platform.startsWith("Mac")) { - // Before anything else on this path. The toolkit, if it is ever woken, asks - // the main thread to run the application, and this thread is the one drawing - // the frames: that request is delivered on the first frame and never comes - // back. Saying up front that there is no display to open keeps the toolkit - // from asking, and nothing on this path wants one. + // A window of our own means there is no display for the toolkit to open. Saying so up + // front keeps it from being woken: on macOS it would ask the main thread to run the + // application, and this thread is the one drawing the frames, so that request would + // be delivered on the first frame and never come back. + if (!platform.startsWith("Linux") || linuxOwnWindow) { System.setProperty("java.awt.headless", "true") - runAppKitWindow(autoExitMillis) - return@rendererRun 0 - } - if (System.getenv("DXC_WIN32_WINDOW") != null && platform.startsWith("Windows")) { - runWin32Window() - return@rendererRun 0 } - if (System.getenv("DXC_X11_WINDOW") != null && platform.startsWith("Linux")) { - runX11Window() - return@rendererRun 0 + when { + platform.startsWith("Mac") -> { + runAppKitWindow(autoExitMillis) + return@rendererRun 0 + } + platform.startsWith("Windows") -> { + runWin32Window(autoExitMillis) + return@rendererRun 0 + } + linuxOwnWindow -> { + runX11Window(autoExitMillis) + return@rendererRun 0 + } } dev.darkpyonix.composerust.ui.node.platformWindowMaterial = { asked -> setWindowMaterial(if (asked) 1 else 0) } - // Whether asking for a material actually put anything behind the window. The - // toolkit's window cannot, on any platform that still opens one, so its design - // system draws for an opaque window rather than for a desktop that never shows - // through. dev.darkpyonix.composerust.runtime.platformBacksWindowWithMaterial = { false } - // Lets automated smoke tests close the window; unset in normal use. + // The toolkit's window, which Linux still opens by default. runRenderer(autoExitMillis) { NativeHostConnection() } diff --git a/renderer/desktop/src/SyntheticInput.kt b/renderer/desktop/src/SyntheticInput.kt index 493bf017c..a7c773e99 100644 --- a/renderer/desktop/src/SyntheticInput.kt +++ b/renderer/desktop/src/SyntheticInput.kt @@ -1,6 +1,7 @@ package dev.darkpyonix.composerust.ui.platform import androidx.compose.ui.unit.IntSize +import java.lang.System /** * Input and resizing made up from inside the window, for measuring without a hand. @@ -8,15 +9,21 @@ import androidx.compose.ui.unit.IntSize * A session with no accessibility permission cannot post a click, type a letter or drag * an edge, and the two things worth timing here are exactly those: how long a typed letter * takes to show, and what a drag does to the picture. Asked for with `DXC_SYNTH`, a - * comma separated list of `type` and `resize`; unset, nothing here runs. + * comma separated list of `type`, `resize` and `exit`; unset, nothing here runs. * * `type` presses on the first text field the window describes, then commits one letter * every 700 ms from two seconds in. `resize` takes the window from its size to 360x420 in - * 60 steps, six seconds in, from a single call that holds the thread as a drag does. + * 60 steps, six seconds in (`DXC_SYNTH_RESIZE_AT` seconds, where set), from a single call + * that holds the thread as a drag does. `exit` ends the window two seconds after the resize + * (`DXC_SYNTH_EXIT_AT` seconds in, where set), so a run measures and finishes by itself and + * the summary at the end of the loop is printed. */ internal class SyntheticInput(what: String) { private val wantsType = "type" in what.split(',') private val wantsResize = "resize" in what.split(',') + private val wantsExit = "exit" in what.split(',') + private val resizeAt = System.getenv("DXC_SYNTH_RESIZE_AT")?.toDoubleOrNull() ?: 6.0 + private val exitAt = System.getenv("DXC_SYNTH_EXIT_AT")?.toDoubleOrNull() ?: (resizeAt + 2.0) private val started = System.nanoTime() private var field: AccessibleElement? = null private var clicked = false @@ -66,11 +73,15 @@ internal class SyntheticInput(what: String) { /** True once, when it is time to take the window through its sizes. */ fun resizeDue(now: Long): Boolean { - if (!wantsResize || resized || seconds(now) < 6.0) return false + if (!wantsResize || resized || seconds(now) < resizeAt) return false resized = true return true } + /** True once the run has done what it was asked to and should close the window. */ + fun exitDue(now: Long): Boolean = + wantsExit && (!wantsResize || resized) && seconds(now) >= exitAt + /** Remembers where the field is, and says when the echo line has caught up. */ fun noteElements(elements: List) { if (field == null) field = elements.firstOrNull { it.role == ElementRole.FIELD } diff --git a/renderer/desktop/src/TextInputSession.kt b/renderer/desktop/src/TextInputSession.kt index 6f8ce234b..42776a37d 100644 --- a/renderer/desktop/src/TextInputSession.kt +++ b/renderer/desktop/src/TextInputSession.kt @@ -59,6 +59,16 @@ class NativeTextInput { * without committing anything, which is what an input method asks for when the reader * backs out of it. */ + /** + * Where the caret is, in the scene's pixels from its top left, or null where no field + * has the keyboard or has not been laid out yet. The input method opens its candidate + * window there. + */ + fun caretSpot(): androidx.compose.ui.geometry.Offset? { + val rect = session?.focusedRectInRoot?.invoke() ?: return null + return androidx.compose.ui.geometry.Offset(rect.left, rect.bottom) + } + fun compose(text: String) { val request = session ?: return request.onEditCommand(listOf(SetComposingTextCommand(AnnotatedString(text), 1))) diff --git a/renderer/desktop/src/Win32Window.kt b/renderer/desktop/src/Win32Window.kt index 8911fe268..8f93956f1 100644 --- a/renderer/desktop/src/Win32Window.kt +++ b/renderer/desktop/src/Win32Window.kt @@ -272,9 +272,11 @@ private fun forgetFrameCallback() = setDrawCallback( * composed, painted by Skia into a swapchain buffer Direct3D gave us, reaching the screen * with no toolkit anywhere between. * - * Reached by setting `DXC_WIN32_WINDOW`, so the ordinary path is untouched. + * [autoExitMillis] closes the window by itself after that long, for runs nobody watches. */ -internal fun runWin32Window() { +internal fun runWin32Window(autoExitMillis: Long? = null) { + // The window shows nothing behind itself, so a design is told it has no material. + installNativeWindowHooks(backdropSupported = false) // The Host is started before there is a window, because what the window should look // like is in its first batch and a window cannot be told afterwards. Started on this // thread, which is the one every later call to it is made from: the boundary is a @@ -282,11 +284,15 @@ internal fun runWin32Window() { val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window - val window = openWin32Window( - asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", - if (asked != null && asked.width > 0) asked.width else 520, - if (asked != null && asked.height > 0) asked.height else 360, + val options = nativeWindowOptions(host, backdropSupported = false) + configureNativeWindow( + resizable = options.resizable, + minWidth = options.minWidth, + minHeight = options.minHeight, + systemChrome = options.systemChrome, + backdrop = false, ) + val window = openWin32Window(options.title, options.width, options.height) if (window == null) { System.err.println("compose-rust: this machine has no Direct3D 12 adapter") host.shutdown() @@ -330,7 +336,20 @@ internal fun runWin32Window() { ) // The application's own tree, drawn by the same interpreter the toolkit path uses. // Nothing in it knows which of the two it is running on, which is the point. - scene.setContent { dev.darkpyonix.composerust.runtime.ComposeRustContent(host) } + // The strip the system's caption occupied, which the window gave up to the content. The + // buttons in it are the application's own, so they are the window's to operate. + val caption = if (options.systemChrome) { + dev.darkpyonix.composerust.runtime.WindowCaption.None + } else { + dev.darkpyonix.composerust.runtime.WindowCaption(height = windowsCaptionHeight) + } + scene.setContent { + NativeWindowContent( + host, + caption, + actions = if (options.systemChrome) null else nativeWindowActions(), + ) + } // What a frame is, wherever the ask comes from. The loop below is one caller and the // window's own resize handling is the other, and they draw the same frame. @@ -351,6 +370,8 @@ internal fun runWin32Window() { } } registerFrameCallback() + val started = System.nanoTime() + var iconSettled = false try { // Rests only when the last turn found nothing to do. A frame that drew has @@ -358,6 +379,12 @@ internal fun runWin32Window() { // that would halve the rate of anything that animates. var busy = true while (!isWindowClosed()) { + if (autoExitMillis != null && + (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis + ) { + break + } + if (!iconSettled) iconSettled = applyNamedIcon(host, asked?.icon ?: 0) // Cleared before the window is given its turn rather than after. A drag of an // edge draws its frames from inside that turn, and a turn that forgot them // would be a window that said nothing about itself for the length of a drag, @@ -375,6 +402,14 @@ internal fun runWin32Window() { } // Told which desktop it is, because the key numbers differ: the shared // table is macOS's, and without this Home arrives as Enter. + if (event.kind == WindowEvent.FILES_ENTERED || + event.kind == WindowEvent.FILES_DROPPED || + event.kind == WindowEvent.FILES_EXITED + ) { + routeFileDrop(event.kind, androidx.compose.ui.geometry.Offset(event.x, event.y)) { + readDroppedPaths() + } + } scene.receive(event, win32 = true) textInput.receive(event) heard = true @@ -389,6 +424,7 @@ internal fun runWin32Window() { // tree that changed on the last one is a tree nobody has been told about, and // a window that has gone still is exactly where that would be forgotten. semantics.pushIfChanged(afterDrawing = drew) + reportCaret(textInput) busy = heard || drew } } finally { @@ -470,5 +506,6 @@ private fun drawFrame( return fitted } +private const val NANOS_PER_MILLI = 1_000_000L private const val FRAME_SECONDS = 0.016 private const val FRAME_NANOS = 16_000_000L diff --git a/renderer/desktop/src/WindowEvents.kt b/renderer/desktop/src/WindowEvents.kt index b9fc74809..373d70a02 100644 --- a/renderer/desktop/src/WindowEvents.kt +++ b/renderer/desktop/src/WindowEvents.kt @@ -54,6 +54,13 @@ data class WindowEvent( const val FILES_ENTERED = 10 const val FILES_DROPPED = 11 const val FILES_EXITED = 12 + + // An input method's preedit callbacks as the X11 window heard them: a composition began, + // a run of characters was replaced (first in `keyCode`, how many in `codePoint`, the caret + // in `x`, the new characters in `text`), and the composition ended. + const val PREEDIT_START = 13 + const val PREEDIT_DRAW = 14 + const val PREEDIT_DONE = 15 } } @@ -120,6 +127,44 @@ internal fun ComposeScene.receive(event: WindowEvent, win32: Boolean = false) { internal fun win32ComposeKey(virtualKey: Int): Key = when (virtualKey) { + // The letters and digits, which are their own numbers in a virtual key. Without them + // control with C, V, X or Z reaches Compose as a key it does not know. + 0x41 -> Key.A + 0x42 -> Key.B + 0x43 -> Key.C + 0x44 -> Key.D + 0x45 -> Key.E + 0x46 -> Key.F + 0x47 -> Key.G + 0x48 -> Key.H + 0x49 -> Key.I + 0x4A -> Key.J + 0x4B -> Key.K + 0x4C -> Key.L + 0x4D -> Key.M + 0x4E -> Key.N + 0x4F -> Key.O + 0x50 -> Key.P + 0x51 -> Key.Q + 0x52 -> Key.R + 0x53 -> Key.S + 0x54 -> Key.T + 0x55 -> Key.U + 0x56 -> Key.V + 0x57 -> Key.W + 0x58 -> Key.X + 0x59 -> Key.Y + 0x5A -> Key.Z + 0x30 -> Key.Zero + 0x31 -> Key.One + 0x32 -> Key.Two + 0x33 -> Key.Three + 0x34 -> Key.Four + 0x35 -> Key.Five + 0x36 -> Key.Six + 0x37 -> Key.Seven + 0x38 -> Key.Eight + 0x39 -> Key.Nine 0x0D -> Key.Enter 0x09 -> Key.Tab 0x20 -> Key.Spacebar diff --git a/renderer/desktop/src/WindowSizeHints.kt b/renderer/desktop/src/WindowSizeHints.kt new file mode 100644 index 000000000..366d947f6 --- /dev/null +++ b/renderer/desktop/src/WindowSizeHints.kt @@ -0,0 +1,36 @@ +package dev.darkpyonix.composerust.ui.platform + +/** + * The sizes an X11 window tells its window manager it may take, in pixels. + * + * What the application asked for, turned into the two hints a manager reads: a smallest size, + * and a largest. A window that cannot be resized is held to the size it opened at, which is + * both at once; otherwise only the smallest is stated, and only when the application named one. + * One rule for both X11 windows, because a window that lets itself be dragged below the size its + * application asked for is a layout that has stopped fitting. + */ +internal data class SizeHints( + val min: Pair?, + val max: Pair?, +) + +/** The hints for a window, or null where there is nothing to tell the manager. */ +internal fun sizeHintsFor( + minWidth: Int, + minHeight: Int, + resizable: Boolean, + width: Int, + height: Int, + scale: Float = 1f, +): SizeHints? { + var min: Pair? = null + var max: Pair? = null + if (minWidth > 0 || minHeight > 0) { + min = (minWidth * scale + 0.5f).toInt() to (minHeight * scale + 0.5f).toInt() + } + if (!resizable) { + min = width to height + max = width to height + } + return if (min == null && max == null) null else SizeHints(min, max) +} diff --git a/renderer/desktop/src/X11Keys.kt b/renderer/desktop/src/X11Keys.kt new file mode 100644 index 000000000..83a24f12f --- /dev/null +++ b/renderer/desktop/src/X11Keys.kt @@ -0,0 +1,134 @@ +package dev.darkpyonix.composerust.ui.platform + +// What an X11 server calls a key and a modifier, turned into the numbering the shared reader +// (`composeKey`) understands. The numbering is AppKit's: letters and digits by where they sit +// on a board, special keys by name. +// +// One table for both X11 windows. The window that links the native image hands the keysym +// over untouched, and the Kotlin/Native window reads it from Xlib; both then ask here, so a +// key can no longer mean one thing on one path and another on the other. Plain integers +// rather than Xlib's constants, because this file is compiled for the native image and for +// Kotlin/Native and only the second has the X11 headers. The values are the ones in +// `keysymdef.h` and `X.h`, and the test names the ones that matter. + +/** A key with no meaning of its own. Zero is the A key in this numbering, so it cannot be zero. */ +internal const val NO_KEY = -1 + +/** The key number for an X11 keysym, or [NO_KEY] where the key only types a character. */ +internal fun x11KeyNumber(keysym: Long): Int { + if (keysym in XK_LOWER_A..XK_LOWER_Z) return X11_LETTERS[(keysym - XK_LOWER_A).toInt()] + if (keysym in XK_UPPER_A..XK_UPPER_Z) return X11_LETTERS[(keysym - XK_UPPER_A).toInt()] + if (keysym in XK_ZERO..XK_NINE) return X11_DIGITS[(keysym - XK_ZERO).toInt()] + return when (keysym) { + XK_RETURN, XK_KP_ENTER -> 0x24 + XK_TAB, XK_ISO_LEFT_TAB -> 0x30 + XK_SPACE -> 0x31 + XK_BACKSPACE -> 0x33 + XK_ESCAPE -> 0x35 + XK_DELETE -> 0x75 + XK_LEFT -> 0x7B + XK_RIGHT -> 0x7C + XK_DOWN -> 0x7D + XK_UP -> 0x7E + XK_HOME -> 0x73 + XK_END -> 0x77 + XK_PRIOR -> 0x74 + XK_NEXT -> 0x79 + else -> NO_KEY + } +} + +/** + * What the X11 window's records become for the scene. + * + * The window records what the server and the input method said as it was said: a key as a + * keysym, a state word and the text it typed, and the preedit callbacks as they came. The two + * windows on X11 then turn that into events with the same code, `keyEventsFor` and [ImeSession], + * so the key and the composition mean the same on the native image and on Kotlin/Native. + * + * [resetInput] ends the input method's composition and answers what it kept. It is asked when + * a button goes down while something is being composed, because a click ends the composition + * and keeps what was typed, which is what the other desktops' input methods do. + */ +internal class X11Events(private val resetInput: () -> String) { + private val pending = ArrayList() + private val ime = ImeSession { pending += it } + + fun heard(raw: WindowEvent): List { + pending.clear() + when (raw.kind) { + WindowEvent.KEY_DOWN, WindowEvent.KEY_UP -> pending += keyEventsFor( + press = raw.kind == WindowEvent.KEY_DOWN, + state = raw.modifiers, + keysym = raw.keyCode.toLong() and 0xFFFFFFFFL, + text = raw.text, + ) + WindowEvent.PREEDIT_START -> ime.preeditStart() + WindowEvent.PREEDIT_DRAW -> ime.preeditDraw( + first = raw.keyCode, + length = raw.codePoint, + text = raw.text, + caret = raw.x.toInt(), + ) + WindowEvent.PREEDIT_DONE -> ime.preeditDone() + else -> { + if (raw.kind == WindowEvent.POINTER_DOWN && ime.composing) { + val kept = resetInput() + if (kept.isNotEmpty()) ime.commit(kept) else ime.preeditDone() + } + pending += raw.copy(modifiers = x11Modifiers(raw.modifiers)) + } + } + return ArrayList(pending) + } +} + +/** + * The four modifier bits the shared reader uses, which are the ones an NSEvent carries, from + * the state word an X11 event carries. Super is the key a Linux desktop puts where macOS puts + * Command, so it lands in Command's bit. + */ +internal fun x11Modifiers(state: Int): Int = + (if (state and X11_SHIFT_MASK != 0) 1 shl 17 else 0) or + (if (state and X11_CONTROL_MASK != 0) 1 shl 18 else 0) or + (if (state and X11_MOD1_MASK != 0) 1 shl 19 else 0) or + (if (state and X11_MOD4_MASK != 0) 1 shl 20 else 0) + +/** Whether the state word says a command is being typed rather than text: control, alt or super. */ +internal fun x11IsShortcut(state: Int): Boolean = + state and (X11_CONTROL_MASK or X11_MOD1_MASK or X11_MOD4_MASK) != 0 + +internal const val X11_SHIFT_MASK = 1 shl 0 +internal const val X11_CONTROL_MASK = 1 shl 2 +internal const val X11_MOD1_MASK = 1 shl 3 +internal const val X11_MOD4_MASK = 1 shl 6 + +private const val XK_LOWER_A = 0x61L +private const val XK_LOWER_Z = 0x7AL +private const val XK_UPPER_A = 0x41L +private const val XK_UPPER_Z = 0x5AL +private const val XK_ZERO = 0x30L +private const val XK_NINE = 0x39L +private const val XK_SPACE = 0x20L +private const val XK_BACKSPACE = 0xFF08L +private const val XK_TAB = 0xFF09L +private const val XK_RETURN = 0xFF0DL +private const val XK_ESCAPE = 0xFF1BL +private const val XK_HOME = 0xFF50L +private const val XK_LEFT = 0xFF51L +private const val XK_UP = 0xFF52L +private const val XK_RIGHT = 0xFF53L +private const val XK_DOWN = 0xFF54L +private const val XK_PRIOR = 0xFF55L +private const val XK_NEXT = 0xFF56L +private const val XK_END = 0xFF57L +private const val XK_KP_ENTER = 0xFF8DL +private const val XK_DELETE = 0xFFFFL +private const val XK_ISO_LEFT_TAB = 0xFE20L + +// a to z, then 0 to 9, each as the board numbers them. +private val X11_LETTERS = intArrayOf( + 0x00, 0x0B, 0x08, 0x02, 0x0E, 0x03, 0x05, 0x04, 0x22, 0x26, 0x28, 0x25, 0x2E, + 0x2D, 0x1F, 0x23, 0x0C, 0x0F, 0x01, 0x11, 0x20, 0x09, 0x0D, 0x07, 0x10, 0x06, +) +private val X11_DIGITS = intArrayOf(0x1D, 0x12, 0x13, 0x14, 0x15, 0x17, 0x16, 0x1A, 0x1C, 0x19) diff --git a/renderer/desktop/src/X11Window.kt b/renderer/desktop/src/X11Window.kt index 491b6e96d..37a66b1f5 100644 --- a/renderer/desktop/src/X11Window.kt +++ b/renderer/desktop/src/X11Window.kt @@ -14,6 +14,7 @@ import org.graalvm.nativeimage.c.type.CIntPointer import org.graalvm.nativeimage.c.type.CTypeConversion import org.graalvm.word.Pointer import org.graalvm.word.WordFactory +import androidx.compose.ui.unit.dp // X11 and GLX own the window and framebuffer. The event record, scene input conversion // and window content are shared with the other desktop windows. @@ -29,6 +30,15 @@ private external fun beginFrame(window: Pointer?): Int @CFunction("dxc_native_frame_end") private external fun endFrame(display: Pointer?) +@CFunction("dxc_native_note_present") +private external fun countPresent(width: Int, height: Int) + +@CFunction("dxc_native_debug_resize") +private external fun debugResize( + window: Pointer?, view: Pointer?, fromWidth: Int, fromHeight: Int, toWidth: Int, + toHeight: Int, steps: Int, pauseMicros: Int, +) + class X11NativeWindow internal constructor( val window: Long, val view: Long, @@ -46,6 +56,17 @@ class X11NativeWindow internal constructor( fun beginFrame(): Boolean = beginFrame(WordFactory.pointer(window)) == 0 fun endFrame() = endFrame(WordFactory.pointer(queue)) + + /** Counts a frame at this size for the resize report. See `DXC_REPORT_RESIZE`. */ + internal fun notePresent(width: Int, height: Int) = countPresent(width, height) + + /** Takes the window through the sizes a drag would, for measuring. See `DXC_SYNTH`. */ + internal fun scriptedResize(from: Pair, to: Pair, steps: Int, pauseMicros: Int) { + debugResize( + WordFactory.pointer(window), WordFactory.pointer(view), + from.first, from.second, to.first, to.second, steps, pauseMicros, + ) + } } /** @@ -93,20 +114,27 @@ private const val GL_RGBA8 = 0x8058 * the painter registered below. A loop that drew it on its next turn would be a window * whose edge moves before its content does. * - * Reached by setting `DXC_X11_WINDOW`, so the ordinary path is untouched. + * [autoExitMillis] closes the window by itself after that long, for runs nobody watches. */ -internal fun runX11Window() { +internal fun runX11Window(autoExitMillis: Long? = null) { + // The window shows nothing behind itself, so a design is told it has no material. + installNativeWindowHooks(backdropSupported = false) // The Host is started before there is a window, because what the window should look // like is in its first batch and a window cannot be told afterwards. Started on this // thread, which is the one every later call to it is made from. val host = dev.darkpyonix.composerust.runtime.ComposeRustHost(NativeHostConnection()) host.start() val asked = host.table.window - val window = openX11Window( - asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", - if (asked != null && asked.width > 0) asked.width else 520, - if (asked != null && asked.height > 0) asked.height else 360, + val options = nativeWindowOptions(host, backdropSupported = false) + configureNativeWindow( + resizable = options.resizable, + minWidth = options.minWidth, + minHeight = options.minHeight, + systemChrome = options.systemChrome, + backdrop = false, ) + val x11Events = X11Events(::resetInputMethod) + val window = openX11Window(options.title, options.width, options.height) if (window == null) { System.err.println("compose-rust: X11 or a GLX visual is unavailable") host.shutdown() @@ -124,10 +152,13 @@ internal fun runX11Window() { // the render target, and what the scene is told about the window it is in. var size = androidx.compose.ui.unit.IntSize(measured.width, measured.height) val textInput = NativeTextInput() + // Input and resizing made up from inside the window, for the parity check. Unset, nothing runs. + val synthetic = System.getenv("DXC_SYNTH")?.let { SyntheticInput(it) } val semantics = NativeSemantics { elements -> if (report) { System.err.println("compose-rust: the window has ${elements.size} things to say") } + synthetic?.noteElements(elements) window.describeTo(elements) } // Kept rather than left to the scene. What a scene picks for itself is the toolkit's @@ -142,7 +173,27 @@ internal fun runX11Window() { ) // The application's own tree, drawn by the same interpreter the toolkit path uses. // Nothing in it knows which of the two it is running on, which is the point. - scene.setContent { dev.darkpyonix.composerust.runtime.ComposeRustContent(host) } + // Without the manager's frame the renderer draws the caption: a strip that moves the + // window, the three buttons, and the edges that resize it. The window manager does the + // moving and the resizing; what is drawn here is only where a press starts it. + val framed = options.systemChrome + val caption = if (framed) { + dev.darkpyonix.composerust.runtime.WindowCaption.None + } else { + dev.darkpyonix.composerust.runtime.WindowCaption(height = linuxCaptionHeight) + } + val actions = if (framed) null else nativeWindowActions() + scene.setContent { + NativeWindowContent( + host, + caption, + actions, + drag = { if (!framed) NativeCaptionDrag(linuxCaptionHeight, actions) }, + overlay = { if (!framed && options.resizable) NativeResizeEdges() }, + ) + } + val started = System.nanoTime() + var iconSettled = false // A clock rather than a count of turns, because a turn and a frame are no longer the // same thing: a resize draws its own, and a count only the loop advanced would hand @@ -158,8 +209,10 @@ internal fun runX11Window() { scene.size = fitted } size = fitted + val begun = System.nanoTime() if (drawFrame(window, context, scene, System.nanoTime() - opened, fitted)) { painted = true + LatencyTrace.frameDrawn(System.nanoTime() - begun) } } try { @@ -169,6 +222,13 @@ internal fun runX11Window() { // nothing of a scene that has closed. setX11FramePainter { frames.draw() } while (!isWindowClosed()) { + if (synthetic != null && synthetic.exitDue(System.nanoTime())) break + if (autoExitMillis != null && + (System.nanoTime() - started) / NANOS_PER_MILLI >= autoExitMillis + ) { + break + } + if (!iconSettled) iconSettled = applyNamedIcon(host, asked?.icon ?: 0) // The window's own turn, before anything is read from it. This thread is the // one the display server answers on, so the events of this frame arrive here // or not at all. Waiting the frame's length rather than sleeping afterwards, @@ -181,11 +241,32 @@ internal fun runX11Window() { work.runPending() var heard = false var drew = false - for (event in drainWindowEvents()) { + for (event in synthetic?.due(System.nanoTime(), size) ?: emptyList()) { + if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() + LatencyTrace.mark("synthetic ${event.kind} sent") + scene.receive(event) + textInput.receive(event) + heard = true + } + if (synthetic != null && synthetic.resizeDue(System.nanoTime())) { + LatencyTrace.phase = "resize" + window.scriptedResize(size.width to size.height, 360 to 420, 60, 8_000) + LatencyTrace.phase = "idle" + } + for (event in drainWindowEvents().flatMap { x11Events.heard(it) }) { if (report && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") } + if (event.kind == WindowEvent.FILES_ENTERED || + event.kind == WindowEvent.FILES_DROPPED || + event.kind == WindowEvent.FILES_EXITED + ) { + routeFileDrop(event.kind, androidx.compose.ui.geometry.Offset(event.x, event.y)) { + readDroppedPaths() + } + } scene.receive(event) + textInput.receive(event) heard = true } // Only when there is something to draw. A window that is being resized has @@ -199,11 +280,13 @@ internal fun runX11Window() { // tree that changed on the last one is a tree nobody has been told about, and // a window that has gone still is exactly where that would be forgotten. semantics.pushIfChanged(afterDrawing = drew) + reportCaret(textInput) } } finally { // Before the scene closes. A resize arriving between the two would otherwise ask a // scene that has gone to draw into a context that has gone with it. clearX11FramePainter() + LatencyTrace.summary() scene.close() context.close() host.shutdown() @@ -247,6 +330,7 @@ private fun drawFrame( surface.close() target.close() window.endFrame() + window.notePresent(size.width, size.height) return true } @@ -256,4 +340,8 @@ private fun drawFrame( * A frame at sixty per second. It is a ceiling rather than a pace: anything arriving sooner * ends the wait, and a resize is drawn inside it rather than after it. */ +private const val NANOS_PER_MILLI = 1_000_000L private const val FRAME_SECONDS = 0.016 + +/** The caption strip a window with no frame of the system's draws for itself. */ +internal val linuxCaptionHeight = 32.dp diff --git a/renderer/desktop/src/renderer/CodePoints.kt b/renderer/desktop/src/renderer/CodePoints.kt new file mode 100644 index 000000000..1b64bd931 --- /dev/null +++ b/renderer/desktop/src/renderer/CodePoints.kt @@ -0,0 +1,34 @@ +package dev.darkpyonix.composerust.ui.platform + +// Code points of a string, which the accessibility server counts in and the Kotlin/Native +// window's input method counts in as well. One definition for both: the input method's file +// in the Kotlin/Native renderer declares the same two functions today and drops them when it +// takes these from here. + +/** Appends one code point, as the pair of surrogates it is when it is past the first plane. */ +internal fun StringBuilder.appendPoint(point: Int) { + if (point < 0x10000) { + append(point.toChar()) + } else { + val offset = point - 0x10000 + append((0xD800 + (offset shr 10)).toChar()) + append((0xDC00 + (offset and 0x3FF)).toChar()) + } +} + +/** The code points of a string, with a pair of surrogates read as the one character it is. */ +internal fun codePointsOf(text: String): List { + val points = ArrayList(text.length) + var index = 0 + while (index < text.length) { + val unit = text[index] + if (unit.isHighSurrogate() && index + 1 < text.length && text[index + 1].isLowSurrogate()) { + points += 0x10000 + ((unit.code - 0xD800) shl 10) + (text[index + 1].code - 0xDC00) + index += 2 + } else { + points += unit.code + index += 1 + } + } + return points +} diff --git a/renderer/desktop/test/ParityMeasurementTest.kt b/renderer/desktop/test/ParityMeasurementTest.kt new file mode 100644 index 000000000..ef2dd6509 --- /dev/null +++ b/renderer/desktop/test/ParityMeasurementTest.kt @@ -0,0 +1,40 @@ +package dev.darkpyonix.composerust.test + +import dev.darkpyonix.composerust.ui.platform.SyntheticInput +import dev.darkpyonix.composerust.ui.platform.fixed +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertTrue + +/** + * The pieces of the parity run that are plain logic: how a number is printed, which Kotlin/Native + * has no `format` for, and when the synthetic input does each thing. + */ +class ParityMeasurementTest { + @Test + fun a_number_is_printed_with_the_places_asked_for() { + assertEquals("1.23", fixed(1.2345, 2)) + assertEquals("0.050", fixed(0.05, 3)) + assertEquals("12.00", fixed(12.0, 2)) + assertEquals("0.00", fixed(0.0, 2)) + } + + @Test + fun a_number_below_zero_is_printed_as_zero() { + assertEquals("0.00", fixed(-3.0, 2)) + } + + @Test + fun the_run_exits_only_when_asked_to_and_after_the_resize() { + val quiet = SyntheticInput("type") + assertFalse(quiet.exitDue(System.nanoTime() + 60_000_000_000L), "no exit was asked for") + + val measuring = SyntheticInput("resize,exit") + val later = System.nanoTime() + 60_000_000_000L + assertFalse(measuring.exitDue(later), "the resize has not happened yet") + assertTrue(measuring.resizeDue(later)) + assertTrue(measuring.exitDue(later), "the resize is done and the time has come") + assertFalse(measuring.resizeDue(later), "a resize happens once") + } +} diff --git a/renderer/desktop/test/ShortcutKeysTest.kt b/renderer/desktop/test/ShortcutKeysTest.kt new file mode 100644 index 000000000..de06a24b5 --- /dev/null +++ b/renderer/desktop/test/ShortcutKeysTest.kt @@ -0,0 +1,47 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.input.key.Key +import dev.darkpyonix.composerust.ui.platform.composeKey +import dev.darkpyonix.composerust.ui.platform.win32ComposeKey +import kotlin.test.Test +import kotlin.test.assertEquals + +/** + * Control with C, V, X and Z has to reach Compose as those keys on every desktop. + * + * A key the table does not know used to arrive as key number zero, which is A on the board + * the table is written for, so on the X11 window every one of these selected everything. + * The X11 half is `X11KeysTest`, which checks the table both X11 windows read; this is the + * other half, what those numbers mean. + */ +class ShortcutKeysTest { + + @Test + fun nfr14_the_clipboard_shortcuts_are_their_own_keys_on_the_shared_board() { + assertEquals(Key.C, composeKey(0x08)) + assertEquals(Key.V, composeKey(0x09)) + assertEquals(Key.X, composeKey(0x07)) + assertEquals(Key.Z, composeKey(0x06)) + assertEquals(Key.A, composeKey(0x00)) + } + + @Test + fun nfr14_a_key_with_no_number_is_not_a_letter() { + assertEquals(Key.Unknown, composeKey(-1)) + } + + @Test + fun nfr14_the_clipboard_shortcuts_are_their_own_keys_in_a_windows_virtual_key() { + assertEquals(Key.C, win32ComposeKey('C'.code)) + assertEquals(Key.V, win32ComposeKey('V'.code)) + assertEquals(Key.X, win32ComposeKey('X'.code)) + assertEquals(Key.Z, win32ComposeKey('Z'.code)) + assertEquals(Key.Five, win32ComposeKey('5'.code)) + } + + @Test + fun nfr14_digits_are_digits_on_the_shared_board() { + assertEquals(Key.One, composeKey(0x12)) + assertEquals(Key.Zero, composeKey(0x1D)) + } +} diff --git a/renderer/desktop/test/Win32ImeKeyTest.kt b/renderer/desktop/test/Win32ImeKeyTest.kt index f8256f3fa..87f50c0d1 100644 --- a/renderer/desktop/test/Win32ImeKeyTest.kt +++ b/renderer/desktop/test/Win32ImeKeyTest.kt @@ -14,6 +14,8 @@ class Win32ImeKeyTest { assertEquals(Key.DirectionLeft, win32ComposeKey(0x25)) assertEquals(Key.DirectionRight, win32ComposeKey(0x27)) assertEquals(Key.Delete, win32ComposeKey(0x2E)) - assertEquals(Key.Unknown, win32ComposeKey(0x41)) + // The letters are their own virtual keys, so control with C reaches Compose as C. + assertEquals(Key.A, win32ComposeKey(0x41)) + assertEquals(Key.Unknown, win32ComposeKey(0x07)) } } diff --git a/renderer/desktop/test/WindowSizeHintsTest.kt b/renderer/desktop/test/WindowSizeHintsTest.kt new file mode 100644 index 000000000..0c8933157 --- /dev/null +++ b/renderer/desktop/test/WindowSizeHintsTest.kt @@ -0,0 +1,35 @@ +package dev.darkpyonix.composerust.test + +import dev.darkpyonix.composerust.ui.platform.SizeHints +import dev.darkpyonix.composerust.ui.platform.sizeHintsFor +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNull + +/** What an X11 window tells its window manager about the sizes it may take. */ +class WindowSizeHintsTest { + @Test + fun a_window_that_asked_for_nothing_tells_the_manager_nothing() { + assertNull(sizeHintsFor(0, 0, true, 520, 360)) + } + + @Test + fun a_smallest_size_is_stated_and_a_largest_is_not() { + assertEquals(SizeHints(300 to 260, null), sizeHintsFor(300, 260, true, 520, 360)) + } + + @Test + fun one_axis_may_be_named_without_the_other() { + assertEquals(SizeHints(300 to 0, null), sizeHintsFor(300, 0, true, 520, 360)) + } + + @Test + fun a_window_that_cannot_be_resized_is_held_to_the_size_it_opened_at() { + assertEquals(SizeHints(520 to 360, 520 to 360), sizeHintsFor(300, 260, false, 520, 360)) + } + + @Test + fun a_smallest_size_is_scaled_to_pixels_and_rounded() { + assertEquals(SizeHints(451 to 391, null), sizeHintsFor(300, 260, true, 520, 360, scale = 1.5f)) + } +} diff --git a/renderer/desktop/test/X11EventsTest.kt b/renderer/desktop/test/X11EventsTest.kt new file mode 100644 index 000000000..8666f56d0 --- /dev/null +++ b/renderer/desktop/test/X11EventsTest.kt @@ -0,0 +1,95 @@ +package dev.darkpyonix.composerust.test + +import dev.darkpyonix.composerust.ui.platform.WindowEvent +import dev.darkpyonix.composerust.ui.platform.X11Events +import dev.darkpyonix.composerust.ui.platform.X11_CONTROL_MASK +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertTrue + +/** + * What the X11 window's records become, which is the same code the Kotlin/Native window runs + * on what it reads from Xlib. A composition and a key are checked here with no display and no + * input method. + */ +class X11EventsTest { + private fun record( + kind: Int, + keyCode: Int = 0, + modifiers: Int = 0, + codePoint: Int = 0, + x: Float = 0f, + text: String = "", + ) = WindowEvent(kind, x, 0f, 0, modifiers, keyCode, codePoint, text) + + private fun events(reset: () -> String = { "" }) = X11Events(reset) + + @Test + fun a_plain_letter_is_one_key_event_carrying_its_character() { + val heard = events().heard(record(WindowEvent.KEY_DOWN, keyCode = 'a'.code, text = "a")) + assertEquals(1, heard.size) + assertEquals(WindowEvent.KEY_DOWN, heard[0].kind) + assertEquals('a'.code, heard[0].codePoint) + assertEquals(0x00, heard[0].keyCode, "a is the A key on the shared board") + } + + @Test + fun control_with_c_is_the_c_key_and_types_nothing() { + val heard = events().heard( + record(WindowEvent.KEY_DOWN, keyCode = 'c'.code, modifiers = X11_CONTROL_MASK, text = "\u0003"), + ) + assertEquals(1, heard.size) + assertEquals(0x08, heard[0].keyCode) + assertEquals(1 shl 18, heard[0].modifiers) + assertEquals("", heard[0].text) + } + + @Test + fun a_committed_word_is_text_with_no_key_behind_it() { + val heard = events().heard(record(WindowEvent.KEY_DOWN, keyCode = 0, text = "한글")) + assertEquals(listOf(WindowEvent.TEXT_COMMIT to "한글"), heard.map { it.kind to it.text }) + } + + @Test + fun a_composition_is_handed_over_as_the_whole_run_every_time() { + val x11 = events() + assertTrue(x11.heard(record(WindowEvent.PREEDIT_START)).isEmpty()) + val first = x11.heard(record(WindowEvent.PREEDIT_DRAW, keyCode = 0, codePoint = 0, x = 1f, text = "ㅎ")) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to "ㅎ"), first.map { it.kind to it.text }) + val second = x11.heard(record(WindowEvent.PREEDIT_DRAW, keyCode = 0, codePoint = 1, x = 1f, text = "하")) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to "하"), second.map { it.kind to it.text }) + val third = x11.heard(record(WindowEvent.PREEDIT_DRAW, keyCode = 1, codePoint = 0, x = 2f, text = "ㄴ")) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to "하ㄴ"), third.map { it.kind to it.text }) + val done = x11.heard(record(WindowEvent.PREEDIT_DONE)) + assertEquals(listOf(WindowEvent.TEXT_COMPOSE to ""), done.map { it.kind to it.text }) + } + + @Test + fun a_click_while_composing_ends_the_composition_and_keeps_what_was_typed() { + var resets = 0 + val x11 = events { resets++; "하" } + x11.heard(record(WindowEvent.PREEDIT_START)) + x11.heard(record(WindowEvent.PREEDIT_DRAW, text = "하", x = 1f)) + val heard = x11.heard(record(WindowEvent.POINTER_DOWN, x = 10f)) + assertEquals(1, resets) + assertEquals( + listOf(WindowEvent.TEXT_COMPOSE, WindowEvent.TEXT_COMMIT, WindowEvent.POINTER_DOWN), + heard.map { it.kind }, + ) + assertEquals("하", heard[1].text) + } + + @Test + fun a_click_with_nothing_composing_asks_nothing_of_the_input_method() { + var resets = 0 + val heard = events { resets++; "x" }.heard(record(WindowEvent.POINTER_DOWN, x = 10f)) + assertEquals(0, resets) + assertEquals(listOf(WindowEvent.POINTER_DOWN), heard.map { it.kind }) + } + + @Test + fun a_pointer_record_has_its_modifiers_in_the_shared_bits() { + val heard = events().heard(record(WindowEvent.POINTER_MOVE, modifiers = X11_CONTROL_MASK)) + assertEquals(1 shl 18, heard[0].modifiers) + } +} diff --git a/renderer/desktop/test/X11KeysTest.kt b/renderer/desktop/test/X11KeysTest.kt new file mode 100644 index 000000000..4597806e0 --- /dev/null +++ b/renderer/desktop/test/X11KeysTest.kt @@ -0,0 +1,93 @@ +package dev.darkpyonix.composerust.test + +import androidx.compose.ui.input.key.Key +import dev.darkpyonix.composerust.ui.platform.NO_KEY +import dev.darkpyonix.composerust.ui.platform.X11_CONTROL_MASK +import dev.darkpyonix.composerust.ui.platform.X11_MOD1_MASK +import dev.darkpyonix.composerust.ui.platform.X11_MOD4_MASK +import dev.darkpyonix.composerust.ui.platform.X11_SHIFT_MASK +import dev.darkpyonix.composerust.ui.platform.composeKey +import dev.darkpyonix.composerust.ui.platform.x11IsShortcut +import dev.darkpyonix.composerust.ui.platform.x11KeyNumber +import dev.darkpyonix.composerust.ui.platform.x11Modifiers +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertNotEquals + +/** + * The X11 key table both X11 windows read. A shortcut is matched by which key it is, so each + * letter a shortcut uses has to arrive as that letter on every path. + */ +class X11KeysTest { + private fun key(keysym: Long) = composeKey(x11KeyNumber(keysym)) + + @Test + fun the_editing_shortcut_letters_arrive_as_themselves() { + assertEquals(Key.C, key('c'.code.toLong())) + assertEquals(Key.C, key('C'.code.toLong())) + assertEquals(Key.V, key('v'.code.toLong())) + assertEquals(Key.X, key('x'.code.toLong())) + assertEquals(Key.Z, key('z'.code.toLong())) + assertEquals(Key.A, key('a'.code.toLong())) + } + + @Test + fun every_letter_and_digit_has_its_own_key() { + val letters = ('a'..'z').map { x11KeyNumber(it.code.toLong()) } + assertEquals(26, letters.toSet().size) + assertEquals(10, ('0'..'9').map { x11KeyNumber(it.code.toLong()) }.toSet().size) + assertEquals(Key.Five, key('5'.code.toLong())) + assertEquals(Key.Zero, key('0'.code.toLong())) + } + + @Test + fun named_keys_and_their_second_keysyms() { + assertEquals(Key.Enter, key(0xFF0DL)) + assertEquals(Key.Enter, key(0xFF8DL)) + assertEquals(Key.Tab, key(0xFF09L)) + assertEquals(Key.Tab, key(0xFE20L)) + assertEquals(Key.Spacebar, key(0x20L)) + assertEquals(Key.Backspace, key(0xFF08L)) + assertEquals(Key.Escape, key(0xFF1BL)) + assertEquals(Key.Delete, key(0xFFFFL)) + assertEquals(Key.DirectionLeft, key(0xFF51L)) + assertEquals(Key.DirectionUp, key(0xFF52L)) + assertEquals(Key.DirectionRight, key(0xFF53L)) + assertEquals(Key.DirectionDown, key(0xFF54L)) + assertEquals(Key.MoveHome, key(0xFF50L)) + assertEquals(Key.MoveEnd, key(0xFF57L)) + assertEquals(Key.PageUp, key(0xFF55L)) + assertEquals(Key.PageDown, key(0xFF56L)) + } + + @Test + fun a_key_with_no_meaning_is_minus_one_never_zero() { + // Zero is the A key, so an unknown key answering zero arrived as control with A. + assertEquals(NO_KEY, x11KeyNumber(0xFD00L)) + assertEquals(NO_KEY, x11KeyNumber(0xFFBEL)) + assertNotEquals(0, x11KeyNumber(0xFFBEL)) + assertEquals(Key.Unknown, key(0xFFBEL)) + } + + @Test + fun each_modifier_lands_in_its_own_bit() { + val shift = 1 shl 17 + val control = 1 shl 18 + val option = 1 shl 19 + val command = 1 shl 20 + assertEquals(shift, x11Modifiers(X11_SHIFT_MASK)) + assertEquals(control, x11Modifiers(X11_CONTROL_MASK)) + assertEquals(option, x11Modifiers(X11_MOD1_MASK)) + assertEquals(command, x11Modifiers(X11_MOD4_MASK)) + assertEquals(control or shift, x11Modifiers(X11_CONTROL_MASK or X11_SHIFT_MASK)) + assertEquals(0, x11Modifiers(0)) + } + + @Test + fun control_alt_and_super_make_a_shortcut_and_shift_does_not() { + assertEquals(true, x11IsShortcut(X11_CONTROL_MASK)) + assertEquals(true, x11IsShortcut(X11_MOD1_MASK)) + assertEquals(true, x11IsShortcut(X11_MOD4_MASK)) + assertEquals(false, x11IsShortcut(X11_SHIFT_MASK)) + } +} diff --git a/renderer/linux/cinterop/include/appkit_resize.h b/renderer/linux/cinterop/include/appkit_resize.h new file mode 120000 index 000000000..3797ed99f --- /dev/null +++ b/renderer/linux/cinterop/include/appkit_resize.h @@ -0,0 +1 @@ +../../../desktop/c/appkit_resize.h \ No newline at end of file diff --git a/renderer/linux/cinterop/resize.def b/renderer/linux/cinterop/resize.def new file mode 100644 index 000000000..2955e15cb --- /dev/null +++ b/renderer/linux/cinterop/resize.def @@ -0,0 +1,6 @@ +# The resize accounting the AppKit window counts with, header only and without any window system +# in it, so that the Kotlin/Native window counts a drag exactly as the native image windows do. +# `include/appkit_resize.h` is a link to the one copy in `desktop/c/`. +package = resize + +headers = appkit_resize.h diff --git a/renderer/linux/src/InputMethod.kt b/renderer/linux/src/InputMethod.kt index c9768e764..9aa57a924 100644 --- a/renderer/linux/src/InputMethod.kt +++ b/renderer/linux/src/InputMethod.kt @@ -2,184 +2,9 @@ package dev.darkpyonix.composerust.ui.platform -import androidx.compose.ui.geometry.Rect -import x11.ControlMask -import x11.KeySym -import x11.Mod1Mask -import x11.Mod4Mask - -// What an input method says, turned into what the shared reader hears. -// -// Apart from the XIM calls in `XimContext.kt` because these are the part that can be wrong -// without anything looking wrong. A preedit edit applied at the wrong offset still shows -// letters; a commit that is dropped when a syllable is finished still shows the syllable -// that was being composed. Both are plain arithmetic on text, and arithmetic is checkable -// without an input method daemon. -// -// Nothing here reaches Rust. What an input method produces is a `WindowEvent` of the kinds -// the window already has, recorded into the same log as a pointer move, and it ends in the -// text input session Compose opened for the focused field. The Host is told a field changed -// by the field, the way it is for a key typed on any other platform. - -/** - * The text an input method is still composing, kept the way X's preedit callbacks describe - * it: as edits to a run of characters. - * - * The draw callback does not send the whole string. It says "replace [length] characters - * from [first] with this", so a client has to hold the run to apply that to. Counted in code - * points and not in UTF-16 units, because that is what the protocol counts: a Hangul syllable - * is one, and so is a character outside the Basic Multilingual Plane, which is two units. - */ -internal class PreeditBuffer { - private val points = ArrayList() - - /** Where the input method says the caret is inside the run, in characters. */ - var caret = 0 - private set - - val isEmpty: Boolean get() = points.isEmpty() - - /** The run as a string, in the encoding Compose reads. */ - val text: String - get() = buildString { for (point in points) appendPoint(point) } - - /** - * Applies one draw callback. - * - * A start past the end is clamped to the end rather than rejected: a client that threw - * here would be a window that dies in the middle of someone's sentence, and appending is - * what an input method that miscounted meant. - */ - fun replace(first: Int, length: Int, with: String, newCaret: Int) { - val start = first.coerceIn(0, points.size) - val end = (start + length.coerceAtLeast(0)).coerceAtMost(points.size) - for (index in end - 1 downTo start) points.removeAt(index) - points.addAll(start, codePointsOf(with)) - caret = newCaret.coerceIn(0, points.size) - } - - fun clear() { - points.clear() - caret = 0 - } -} - -/** - * One client's side of an input method: the callbacks it makes, and what each of them means. - * - * Takes [emit] rather than a window, so that a test hands it a list and a window hands it the - * log every other event goes into. Everything arrives on the thread that reads the display - * server, from inside the one call that reads it, so nothing here is ever re-entered and - * nothing needs a lock. - */ -internal class ImeSession(private val emit: (WindowEvent) -> Unit) { - private val preedit = PreeditBuffer() - - /** Whether something is being composed, which is what a click has to finish. */ - val composing: Boolean get() = !preedit.isEmpty - - /** The input method begins a composition. Nothing is shown until it draws. */ - fun preeditStart() { - preedit.clear() - } - - /** - * The composition changed. Compose is given the whole run each time, because that is what - * its command replaces: the marked text is the run, and what was marked before is gone. - */ - fun preeditDraw(first: Int, length: Int, text: String, caret: Int) { - preedit.replace(first, length, text, caret) - emit(imeEvent(WindowEvent.TEXT_COMPOSE, preedit.text)) - } - - /** The composition ended without committing anything: the reader backed out of it. */ - fun preeditDone() { - if (preedit.isEmpty) return - preedit.clear() - emit(imeEvent(WindowEvent.TEXT_COMPOSE, "")) - } - - /** - * Finished text. Whatever was being composed is taken out first: some input methods clear - * the preedit and then commit, and some commit and never clear it, and a field must not be - * left holding both the syllable and the mark that was over it. - */ - fun commit(text: String) { - if (text.isEmpty()) return - preeditDone() - emit(imeEvent(WindowEvent.TEXT_COMMIT, text)) - } -} - -/** A record for something an input method said: no position, no key, only the text. */ -internal fun imeEvent(kind: Int, text: String) = WindowEvent( - kind = kind, - x = 0f, - y = 0f, - buttons = 0, - modifiers = 0, - keyCode = 0, - codePoint = 0, - text = text, -) - -/** - * What one key press or release becomes, given what the keyboard and the input method said it - * typed. - * - * [text] is whatever came back from asking for the characters of the event: empty for a key - * that types nothing, one character for most, and several for an input method that committed - * a word. Three cases follow from it, and each exists because of a way this went wrong. - * - * - A key that types one plain ASCII character keeps carrying it in the key event, which is - * how every field here has been typed into so far. Moving it would change what a shortcut - * sees. - * - Anything else printable is committed as text and the key event carries no character. A - * Hangul syllable put in a key event would be read as one code unit by a field that expects - * a character from a keyboard. - * - An event with no key behind it at all, which is what an input method sends to say it has - * committed, produces the commit and no key event: there is nothing for a shortcut to - * see, and a key with no name pressed in a text field would only confuse a handler. - */ -internal fun keyEventsFor( - press: Boolean, - state: UInt, - keysym: KeySym, - text: String, -): List { - val key = platformKey(keysym) - val modifiers = modifiersOf(state) - if (!press) { - return listOf( - WindowEvent(WindowEvent.KEY_UP, 0f, 0f, 0, modifiers, key, 0, ""), - ) - } - val first = text.firstOrNull() - val printable = first != null && first.code >= FIRST_PRINTABLE && first.code != DELETE_CHARACTER - val plain = text.length == 1 && first!!.code in FIRST_PRINTABLE until DELETE_CHARACTER - val shortcut = state.toInt() and (ControlMask or Mod1Mask or Mod4Mask) != 0 - val down = WindowEvent( - WindowEvent.KEY_DOWN, 0f, 0f, 0, modifiers, key, - if (plain) first!!.code else 0, "", - ) - if (!printable || plain) return listOf(down) - if (shortcut) return listOf(down) - val commit = imeEvent(WindowEvent.TEXT_COMMIT, text) - return if (keysym == 0UL) listOf(commit) else listOf(down, commit) -} - -/** - * Where an input method should put its candidate window: under the caret, in the window's own - * pixels, or null where the field has not been laid out yet. - * - * The rectangle is Compose's own answer for the focused field's caret, in the root's units. - * The scene's density is one point to a pixel (see the window's note on it), so no scale - * is applied; [density] is there so that changing that changes one place. - */ -internal fun candidateSpot(caret: Rect?, density: Float): Pair? { - if (caret == null || caret.isEmpty) return null - return (caret.left * density).toInt() to (caret.bottom * density).toInt() -} +// The Xlib side of an input method: which style to ask it for and which locale it needs. +// What it says once it is talking, the preedit model and the key decision, is `ImeComposition.kt`, +// shared with the window that links the native image. /** * Which input style to ask an input method for, out of the ones it offers. @@ -218,33 +43,3 @@ internal fun isUtf8Locale(name: String?): Boolean { val lower = name.lowercase() return lower.endsWith(".utf-8") || lower.endsWith(".utf8") || ".utf-8@" in lower || ".utf8@" in lower } - -private const val FIRST_PRINTABLE = 32 -private const val DELETE_CHARACTER = 0x7F - -internal fun StringBuilder.appendPoint(point: Int) { - if (point < 0x10000) { - append(point.toChar()) - } else { - val offset = point - 0x10000 - append((0xD800 + (offset shr 10)).toChar()) - append((0xDC00 + (offset and 0x3FF)).toChar()) - } -} - -/** The code points of a string, with a pair of surrogates read as the one character it is. */ -internal fun codePointsOf(text: String): List { - val points = ArrayList(text.length) - var index = 0 - while (index < text.length) { - val unit = text[index] - if (unit.isHighSurrogate() && index + 1 < text.length && text[index + 1].isLowSurrogate()) { - points += 0x10000 + ((unit.code - 0xD800) shl 10) + (text[index + 1].code - 0xDC00) - index += 2 - } else { - points += unit.code - index += 1 - } - } - return points -} diff --git a/renderer/linux/src/JvmCompatShims.kt b/renderer/linux/src/JvmCompatShims.kt index e44fa740c..0f1a6ee36 100644 --- a/renderer/linux/src/JvmCompatShims.kt +++ b/renderer/linux/src/JvmCompatShims.kt @@ -2,7 +2,13 @@ package java.lang +import kotlinx.cinterop.alloc +import kotlinx.cinterop.memScoped +import kotlinx.cinterop.ptr import kotlinx.cinterop.toKString +import platform.posix.CLOCK_MONOTONIC +import platform.posix.clock_gettime +import platform.posix.timespec import platform.posix.fflush import platform.posix.fputs import platform.posix.stderr @@ -42,6 +48,13 @@ object System { fun getenv(name: String): String? = platform.posix.getenv(name)?.toKString() + /** The monotonic clock in nanoseconds, which is what the JVM's call of this name answers. */ + fun nanoTime(): Long = memScoped { + val now = alloc() + clock_gettime(CLOCK_MONOTONIC, now.ptr) + now.tv_sec * 1_000_000_000L + now.tv_nsec + } + class ErrorStream internal constructor() { fun println(line: String) { fputs(line + "\n", stderr) diff --git a/renderer/linux/src/LinuxInput.kt b/renderer/linux/src/LinuxInput.kt index f95bd3fc3..b78b50679 100644 --- a/renderer/linux/src/LinuxInput.kt +++ b/renderer/linux/src/LinuxInput.kt @@ -5,33 +5,13 @@ package dev.darkpyonix.composerust.ui.platform import x11.Button1Mask import x11.Button2Mask import x11.Button3Mask -import x11.ControlMask import x11.KeySym -import x11.Mod1Mask -import x11.Mod4Mask -import x11.ShiftMask import x11.XC_crosshair import x11.XC_hand2 import x11.XC_left_ptr import x11.XC_sb_h_double_arrow import x11.XC_sb_v_double_arrow import x11.XC_xterm -import x11.XK_BackSpace -import x11.XK_Delete -import x11.XK_Down -import x11.XK_End -import x11.XK_Escape -import x11.XK_Home -import x11.XK_ISO_Left_Tab -import x11.XK_KP_Enter -import x11.XK_Left -import x11.XK_Next -import x11.XK_Prior -import x11.XK_Return -import x11.XK_Right -import x11.XK_Tab -import x11.XK_Up -import x11.XK_space // What this display server calls a thing, turned into what the shared reader calls it. // @@ -59,13 +39,7 @@ internal fun buttonsOf(state: UInt): Int { * macOS puts Command, which is why it lands in Command's bit: a screen that binds one binds * the other, and neither platform has both. */ -internal fun modifiersOf(state: UInt): Int { - val bits = state.toInt() - return (if (bits and ShiftMask != 0) MODIFIER_SHIFT else 0) or - (if (bits and ControlMask != 0) MODIFIER_CONTROL else 0) or - (if (bits and Mod1Mask != 0) MODIFIER_ALT else 0) or - (if (bits and Mod4Mask != 0) MODIFIER_SUPER else 0) -} +internal fun modifiersOf(state: UInt): Int = x11Modifiers(state.toInt()) /** * The shared reader's number for a key this server named with a keysym. @@ -76,23 +50,7 @@ internal fun modifiersOf(state: UInt): Int { * meaning of its own (zero is the letter A in that numbering), which is most of them: a key that types a character carries the character * beside it and the scene reads that instead. */ -internal fun platformKey(keysym: KeySym): Int = when (keysym.toInt()) { - XK_Return, XK_KP_Enter -> PLATFORM_ENTER - XK_Tab, XK_ISO_Left_Tab -> PLATFORM_TAB - XK_space -> PLATFORM_SPACE - XK_BackSpace -> PLATFORM_BACKSPACE - XK_Escape -> PLATFORM_ESCAPE - XK_Delete -> PLATFORM_DELETE - XK_Left -> PLATFORM_LEFT - XK_Right -> PLATFORM_RIGHT - XK_Down -> PLATFORM_DOWN - XK_Up -> PLATFORM_UP - XK_Home -> PLATFORM_HOME - XK_End -> PLATFORM_END - XK_Prior -> PLATFORM_PAGE_UP - XK_Next -> PLATFORM_PAGE_DOWN - else -> -1 -} +internal fun platformKey(keysym: KeySym): Int = x11KeyNumber(keysym.toLong()) /** * This server's name for a pointer shape, from the number the scene asked with. @@ -121,27 +79,9 @@ internal const val CURSOR_TEXT = 2 internal const val CURSOR_CROSSHAIR = 3 /** From NSEvent.h. The bits the shared reader reads a modifier word's meaning out of. */ -private const val MODIFIER_SHIFT = 1 shl 17 -private const val MODIFIER_CONTROL = 1 shl 18 -private const val MODIFIER_ALT = 1 shl 19 -private const val MODIFIER_SUPER = 1 shl 20 // The shared numbering, which is AppKit's. `composeKey` in the interpreter's own sources is the // other end of each of these. -private const val PLATFORM_ENTER = 0x24 -private const val PLATFORM_TAB = 0x30 -private const val PLATFORM_SPACE = 0x31 -private const val PLATFORM_BACKSPACE = 0x33 -private const val PLATFORM_ESCAPE = 0x35 -private const val PLATFORM_DELETE = 0x75 -private const val PLATFORM_LEFT = 0x7B -private const val PLATFORM_RIGHT = 0x7C -private const val PLATFORM_DOWN = 0x7D -private const val PLATFORM_UP = 0x7E -private const val PLATFORM_HOME = 0x73 -private const val PLATFORM_END = 0x77 -private const val PLATFORM_PAGE_UP = 0x74 -private const val PLATFORM_PAGE_DOWN = 0x79 /** A wheel arrives as a press and a release of a button that does not exist. */ internal const val WHEEL_UP = 4 diff --git a/renderer/linux/src/LinuxRenderer.kt b/renderer/linux/src/LinuxRenderer.kt index 759dce915..dd7cbe326 100644 --- a/renderer/linux/src/LinuxRenderer.kt +++ b/renderer/linux/src/LinuxRenderer.kt @@ -37,11 +37,14 @@ internal fun runRenderer(connection: () -> HostConnection): Int { // What the application asked for. A window that said nothing is listed under whatever this // renderer happens to be called, which is the library's name and not any application's, and a // measurement of zero means it did not ask. - val asked = host.table.window + val options = nativeWindowOptions(host, backdropSupported = false) val window = LinuxWindow.open( - title = asked?.title?.takeIf { it.isNotEmpty() } ?: "compose-rust", - width = if (asked != null && asked.width > 0) asked.width else DEFAULT_WIDTH, - height = if (asked != null && asked.height > 0) asked.height else DEFAULT_HEIGHT, + title = options.title, + width = options.width, + height = options.height, + minWidth = options.minWidth, + minHeight = options.minHeight, + resizable = options.resizable, ) if (window == null) { java.lang.System.err.println( @@ -72,7 +75,3 @@ internal fun runRenderer(connection: () -> HostConnection): Int { } return RendererApi.RUN_OK } - -/** What a window that did not say is opened at, in the units the scene measures in. */ -private const val DEFAULT_WIDTH = 520 -private const val DEFAULT_HEIGHT = 360 diff --git a/renderer/linux/src/LinuxWindow.kt b/renderer/linux/src/LinuxWindow.kt index 1d0f0c95d..7851ccbe1 100644 --- a/renderer/linux/src/LinuxWindow.kt +++ b/renderer/linux/src/LinuxWindow.kt @@ -32,6 +32,7 @@ import kotlinx.cinterop.cValuesOf import kotlinx.cinterop.convert import kotlinx.cinterop.get import kotlinx.cinterop.memScoped +import kotlinx.cinterop.nativeHeap import kotlinx.cinterop.pointed import kotlinx.cinterop.ptr import kotlinx.cinterop.reinterpret @@ -65,7 +66,20 @@ import x11.NotifyNormal import x11.NotifyWhileGrabbed import x11.SubstructureNotifyMask import x11.SubstructureRedirectMask +import x11.PMaxSize +import x11.PMinSize +import x11.XAllocSizeHints import x11.XMapRaised +import x11.XSetWMNormalHints +import x11.XWindowAttributes +import resize.dxc_resize_present_against +import resize.dxc_resize_reset +import resize.dxc_resize_step +import resize.dxc_resize_stale_ratio +import resize.dxc_resize_stats +import resize.dxc_resize_stretched +import x11.XGetWindowAttributes +import x11.XResizeWindow import x11.XSendEvent import x11.GLXContext import x11.GLX_BLUE_SIZE @@ -207,8 +221,20 @@ internal class LinuxWindow private constructor( */ private var described: List = emptyList() + /** + * What a resize cost and what it showed, counted by the header the native image windows + * count with, so a drag is counted the same way on every path. Reported at the end of the + * run when `DXC_REPORT_RESIZE` is set. + */ + private val reportResize = System.getenv("DXC_REPORT_RESIZE") != null + private val resizeStats = nativeHeap.alloc().also { dxc_resize_reset(it.ptr) } + + /** Input and resizing made up from inside the window, for the parity check. Unset, nothing runs. */ + private val synthetic = System.getenv("DXC_SYNTH")?.let { SyntheticInput(it) } + private val semantics = NativeSemantics { elements -> described = elements + synthetic?.noteElements(elements) if (reportFrames) { System.err.println( "compose-rust: the window holds ${described.size} things to say" + @@ -363,11 +389,24 @@ internal class LinuxWindow private constructor( scene.size = size } val nanos = monotonicNanos() - openedAt + val begun = System.nanoTime() val drew = surface.draw(size.width, size.height) { canvas -> scene.render(canvas.asComposeCanvas(), nanos) } if (drew) { painted = true + if (reportResize) { + // The window's size now, asked of the server: a frame at any other size than + // the window has is stale, whatever the last size event said. + memScoped { + val attributes = alloc() + XGetWindowAttributes(display, window, attributes.ptr) + dxc_resize_present_against( + resizeStats.ptr, size.width, size.height, attributes.width, attributes.height, + ) + } + } + LatencyTrace.frameDrawn(System.nanoTime() - begun) // The drawing goes to the server and then the manager is told, in that order. This // is the whole of what keeps a dragged edge attached to what is inside it. sync.frameDrawn() @@ -439,6 +478,16 @@ internal class LinuxWindow private constructor( work.runPending() drained.clear() log.drain(drained) + synthetic?.due(System.nanoTime(), measured)?.forEach { event -> + if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() + LatencyTrace.mark("synthetic ${event.kind} sent") + drained.add(event) + } + if (synthetic != null && synthetic.resizeDue(System.nanoTime())) scriptedResize() + if (synthetic != null && synthetic.exitDue(System.nanoTime())) { + closed = true + continue + } for (event in drained) { if (reportInput && event.kind != WindowEvent.POINTER_MOVE) { System.err.println("compose-rust: window heard $event") @@ -468,10 +517,39 @@ internal class LinuxWindow private constructor( updateInputMethod() } } finally { + LatencyTrace.summary() + if (reportResize) { + System.err.println( + "dxc resize: steps=${resizeStats.steps} presented=${resizeStats.presented} " + + "stale=${resizeStats.stale} stretched=${dxc_resize_stretched(resizeStats.ptr)} " + + "stale_ratio=${fixed(dxc_resize_stale_ratio(resizeStats.ptr), 4)}", + ) + } close() } } + /** + * Takes the window through the sizes a drag would, for measuring: from the size it has to + * 360 by 420 in sixty steps, each asked of the server and its events read before the next, + * so that the frame that belongs to a size is drawn inside the handling of it, as it is + * for a hand on an edge. + */ + private fun scriptedResize() { + LatencyTrace.phase = "resize" + dxc_resize_reset(resizeStats.ptr) + val from = measured + for (step in 1..SYNTHETIC_RESIZE_STEPS) { + val t = step.toDouble() / SYNTHETIC_RESIZE_STEPS + val width = (from.width + (SYNTHETIC_RESIZE_WIDTH - from.width) * t).toInt().coerceAtLeast(1) + val height = (from.height + (SYNTHETIC_RESIZE_HEIGHT - from.height) * t).toInt().coerceAtLeast(1) + XResizeWindow(display, window, width.toUInt(), height.toUInt()) + XFlush(display) + pump(SYNTHETIC_RESIZE_PAUSE_MILLISECONDS) + } + LatencyTrace.phase = "idle" + } + /** * Lets the window answer for itself for a moment, and rests if it has nothing to say. * @@ -602,6 +680,7 @@ internal class LinuxWindow private constructor( return } measured = IntSize(width, height) + dxc_resize_step(resizeStats.ptr, width, height) // Drawn here, inside the handling of the size change, rather than written down // for the next turn of the loop. See this class's own documentation: the strip // of unpainted window a later frame leaves is as wide as the speed of the hand. @@ -668,7 +747,7 @@ internal class LinuxWindow private constructor( val press = event.type == KeyPress val keysym = XLookupKeysym(event.xkey.ptr, 0) val typed = if (!press) "" else xim?.lookup(event) ?: latinText(event) - return keyEventsFor(press, event.xkey.state, keysym, typed) + return keyEventsFor(press, event.xkey.state.toInt(), keysym.toLong(), typed) } /** What the key types without an input method: Latin-1, as `XLookupString` answers. */ @@ -815,7 +894,14 @@ internal class LinuxWindow private constructor( * double buffered visual, has nothing to say beyond that, and a Kotlin exception must not * be allowed to reach the C entry point that called in. */ - fun open(title: String, width: Int, height: Int): LinuxWindow? { + fun open( + title: String, + width: Int, + height: Int, + minWidth: Int = 0, + minHeight: Int = 0, + resizable: Boolean = true, + ): LinuxWindow? { val display = XOpenDisplay(null) ?: return null val screen = XDefaultScreen(display) // Terminated by zero, which is what X's `None` is and what a binding cannot carry @@ -885,6 +971,27 @@ internal class LinuxWindow private constructor( XSetWMProtocols(display, window, protocols, count) } XStoreName(display, window, title) + // What sizes the window may take, told to the manager before the window is mapped + // because that is when it reads them. The rule is the native image window's. + sizeHintsFor(minWidth, minHeight, resizable, width, height, DENSITY)?.let { hints -> + val raw = XAllocSizeHints() + if (raw != null) { + var flags = 0L + hints.min?.let { (smallestWidth, smallestHeight) -> + flags = flags or PMinSize.toLong() + raw.pointed.min_width = smallestWidth + raw.pointed.min_height = smallestHeight + } + hints.max?.let { (largestWidth, largestHeight) -> + flags = flags or PMaxSize.toLong() + raw.pointed.max_width = largestWidth + raw.pointed.max_height = largestHeight + } + raw.pointed.flags = flags + XSetWMNormalHints(display, window, raw) + XFree(raw) + } + } XMapWindow(display, window) XFlush(display) @@ -971,6 +1078,10 @@ internal class LinuxWindow private constructor( private const val DENSITY = 1.0f private const val NANOS_PER_SECOND = 1_000_000_000L + private const val SYNTHETIC_RESIZE_STEPS = 60 + private const val SYNTHETIC_RESIZE_WIDTH = 360 + private const val SYNTHETIC_RESIZE_HEIGHT = 420 + private const val SYNTHETIC_RESIZE_PAUSE_MILLISECONDS = 8 /** * A mask for the low 32 bits: the manager splits the counter value across two words. diff --git a/renderer/linux/src/shared/CodePoints.kt b/renderer/linux/src/shared/CodePoints.kt new file mode 120000 index 000000000..e321fbd29 --- /dev/null +++ b/renderer/linux/src/shared/CodePoints.kt @@ -0,0 +1 @@ +../../../desktop/src/renderer/CodePoints.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/ImeComposition.kt b/renderer/linux/src/shared/ImeComposition.kt new file mode 120000 index 000000000..2cc359b12 --- /dev/null +++ b/renderer/linux/src/shared/ImeComposition.kt @@ -0,0 +1 @@ +../../../desktop/src/ImeComposition.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/LatencyTrace.kt b/renderer/linux/src/shared/LatencyTrace.kt new file mode 120000 index 000000000..fe2933801 --- /dev/null +++ b/renderer/linux/src/shared/LatencyTrace.kt @@ -0,0 +1 @@ +../../../desktop/src/LatencyTrace.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/NativeWindowOptions.kt b/renderer/linux/src/shared/NativeWindowOptions.kt new file mode 120000 index 000000000..b9a4388da --- /dev/null +++ b/renderer/linux/src/shared/NativeWindowOptions.kt @@ -0,0 +1 @@ +../../../desktop/src/NativeWindowOptions.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/SyntheticInput.kt b/renderer/linux/src/shared/SyntheticInput.kt new file mode 120000 index 000000000..beb699088 --- /dev/null +++ b/renderer/linux/src/shared/SyntheticInput.kt @@ -0,0 +1 @@ +../../../desktop/src/SyntheticInput.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/WindowSizeHints.kt b/renderer/linux/src/shared/WindowSizeHints.kt new file mode 120000 index 000000000..de0e65f60 --- /dev/null +++ b/renderer/linux/src/shared/WindowSizeHints.kt @@ -0,0 +1 @@ +../../../desktop/src/WindowSizeHints.kt \ No newline at end of file diff --git a/renderer/linux/src/shared/X11Keys.kt b/renderer/linux/src/shared/X11Keys.kt new file mode 120000 index 000000000..fcd478433 --- /dev/null +++ b/renderer/linux/src/shared/X11Keys.kt @@ -0,0 +1 @@ +../../../desktop/src/X11Keys.kt \ No newline at end of file diff --git a/renderer/linux/test/InputMethodTest.kt b/renderer/linux/test/InputMethodTest.kt index ece2155c0..3dc521fc1 100644 --- a/renderer/linux/test/InputMethodTest.kt +++ b/renderer/linux/test/InputMethodTest.kt @@ -148,7 +148,7 @@ class InputMethodTest { /** What a person typing in English gets: the same key event it always was. */ @Test fun fr5_a_plain_ascii_key_keeps_carrying_its_character() { - val events = keyEventsFor(true, 0u, XK_a.toULong(), "a") + val events = keyEventsFor(true, 0, XK_a.toLong(), "a") assertEquals(1, events.size) assertEquals(WindowEvent.KEY_DOWN, events[0].kind) assertEquals('a'.code, events[0].codePoint) @@ -158,13 +158,13 @@ class InputMethodTest { /** A committed word has no key behind it, so it is text and only text. */ @Test fun fr5_a_commit_the_input_method_forwards_is_text_without_a_key() { - val events = keyEventsFor(true, 0u, 0UL, "한글") + val events = keyEventsFor(true, 0, 0L, "한글") assertEquals(listOf(commitKind to "한글"), events.summary()) } @Test fun fr5_a_key_with_a_non_ascii_character_is_committed_and_also_a_key() { - val events = keyEventsFor(true, 0u, XK_a.toULong(), "é") + val events = keyEventsFor(true, 0, XK_a.toLong(), "é") assertEquals(WindowEvent.KEY_DOWN, events[0].kind) assertEquals(0, events[0].codePoint, "the character is text, not part of the key") assertEquals(commitKind to "é", events[1].kind to events[1].text) @@ -172,25 +172,25 @@ class InputMethodTest { @Test fun fr5_a_control_character_is_a_key_and_never_text() { - val enter = keyEventsFor(true, 0u, XK_Return.toULong(), "\r") + val enter = keyEventsFor(true, 0, XK_Return.toLong(), "\r") assertEquals(1, enter.size) assertEquals(0, enter[0].codePoint) - val backspace = keyEventsFor(true, 0u, XK_BackSpace.toULong(), "\u0008") + val backspace = keyEventsFor(true, 0, XK_BackSpace.toLong(), "\u0008") assertEquals(1, backspace.size) assertEquals(0, backspace[0].codePoint) - val delete = keyEventsFor(true, 0u, 0UL, "\u007F") + val delete = keyEventsFor(true, 0, 0L, "\u007F") assertTrue(delete.none { it.kind == commitKind }) } @Test fun fr5_a_shortcut_never_commits_text() { - val events = keyEventsFor(true, ControlMask.toUInt(), XK_a.toULong(), "é") + val events = keyEventsFor(true, ControlMask, XK_a.toLong(), "é") assertEquals(listOf(WindowEvent.KEY_DOWN), events.map { it.kind }) } @Test fun fr5_a_release_is_a_release_and_carries_no_text() { - val events = keyEventsFor(false, ShiftMask.toUInt(), XK_a.toULong(), "A") + val events = keyEventsFor(false, ShiftMask, XK_a.toLong(), "A") assertEquals(listOf(WindowEvent.KEY_UP), events.map { it.kind }) assertEquals("", events[0].text) } diff --git a/renderer/linux/test/LinuxInputTest.kt b/renderer/linux/test/LinuxInputTest.kt index ae733fca1..b2ab50ecb 100644 --- a/renderer/linux/test/LinuxInputTest.kt +++ b/renderer/linux/test/LinuxInputTest.kt @@ -27,6 +27,8 @@ import x11.XK_Left import x11.XK_Return import x11.XK_Tab import x11.XK_a +import x11.XK_c +import x11.XK_v import x11.XK_space /** @@ -84,15 +86,20 @@ class LinuxInputTest { } /** - * A key that types a letter has no meaning of its own. + * A letter is its own key, because a shortcut is matched by which key it is. * - * Zero rather than a wrong answer. The letter travels beside the key as a character, and a - * key given some other key's number would move the caret instead of typing. + * It used to answer no key, so control with C, V, X, Z or A reached the scene as an unknown + * key and none of the editing shortcuts worked. The GraalVM window's table always had them. + * What a letter types still travels beside it as a character; an unlisted key still has no + * meaning. */ @Test - fun a_key_that_types_a_character_claims_no_meaning() { - assertEquals(-1, platformKey(XK_a.toULong())) - assertEquals(Key.Unknown, composeKey(platformKey(XK_a.toULong()))) + fun a_letter_is_the_key_it_is_and_an_unlisted_key_claims_nothing() { + assertEquals(Key.A, composeKey(platformKey(XK_a.toULong()))) + assertEquals(Key.C, composeKey(platformKey(XK_c.toULong()))) + assertEquals(Key.V, composeKey(platformKey(XK_v.toULong()))) + assertEquals(-1, platformKey(0xFFBEUL)) + assertEquals(Key.Unknown, composeKey(platformKey(0xFFBEUL))) } /** No two named keys share a number, or one of them does what the other means. */ diff --git a/renderer/linux/test/ResizeStatsTest.kt b/renderer/linux/test/ResizeStatsTest.kt new file mode 100644 index 000000000..8efb83d60 --- /dev/null +++ b/renderer/linux/test/ResizeStatsTest.kt @@ -0,0 +1,92 @@ +@file:OptIn(kotlinx.cinterop.ExperimentalForeignApi::class) + +package dev.darkpyonix.composerust.ui.platform + +import kotlinx.cinterop.alloc +import kotlinx.cinterop.nativeHeap +import kotlinx.cinterop.ptr +import resize.dxc_resize_present +import resize.dxc_resize_present_against +import resize.dxc_resize_stale_ratio +import resize.dxc_resize_reset +import resize.dxc_resize_step +import resize.dxc_resize_stats +import resize.dxc_resize_stretched +import kotlin.test.Test +import kotlin.test.assertEquals + +/** + * The resize accounting, fed through the header the Kotlin/Native window links, with the same + * sequences `scripts/tests/appkit-live-resize.test.sh` feeds the same header from C. The two + * have to say the same thing, because the header is the one the native image windows count a + * drag with and the parity run compares what each path reports. + */ +class ResizeStatsTest { + private fun counted(run: (resizeStats: dxc_resize_stats) -> Unit): List { + val stats = nativeHeap.alloc() + try { + dxc_resize_reset(stats.ptr) + run(stats) + return listOf(stats.steps, stats.presented, stats.stale, dxc_resize_stretched(stats.ptr)) + } finally { + nativeHeap.free(stats.rawPtr) + } + } + + @Test + fun a_drag_with_a_frame_at_every_size_counts_nothing_stretched_or_stale() { + val result = counted { stats -> + for (step in 1..60) { + dxc_resize_step(stats.ptr, 480 + step * 2, 640 + step * 2) + dxc_resize_present(stats.ptr, 480 + step * 2, 640 + step * 2) + } + } + assertEquals(listOf(60L, 60L, 0L, 0L), result) + } + + @Test + fun a_drag_with_no_frames_counts_every_size_stretched() { + val result = counted { stats -> + for (step in 1..60) dxc_resize_step(stats.ptr, 480 + step * 2, 640 + step * 2) + } + assertEquals(listOf(60L, 0L, 0L, 60L), result) + } + + @Test + fun a_frame_at_the_previous_size_counts_stale_and_leaves_the_size_stretched() { + val result = counted { stats -> + dxc_resize_step(stats.ptr, 500, 700) + dxc_resize_present(stats.ptr, 480, 640) + } + assertEquals(listOf(1L, 1L, 1L, 1L), result) + } + + @Test + fun a_frame_is_stale_when_it_is_not_the_size_the_window_has_now() { + val stats = nativeHeap.alloc() + try { + dxc_resize_reset(stats.ptr) + // Three frames at the window's size, then one for a size it has already left. + for (width in 400..402) { + dxc_resize_present_against(stats.ptr, width, 300, width, 300) + } + dxc_resize_present_against(stats.ptr, 402, 300, 405, 300) + assertEquals(4L, stats.presented) + assertEquals(1L, stats.stale) + assertEquals(0.25, dxc_resize_stale_ratio(stats.ptr)) + } finally { + nativeHeap.free(stats.rawPtr) + } + } + + @Test + fun nothing_presented_is_a_ratio_of_zero() { + val stats = nativeHeap.alloc() + try { + dxc_resize_reset(stats.ptr) + assertEquals(0.0, dxc_resize_stale_ratio(stats.ptr)) + } finally { + nativeHeap.free(stats.rawPtr) + } + } +} diff --git a/renderer/macos/cinterop/include/appkit_resize.h b/renderer/macos/cinterop/include/appkit_resize.h new file mode 120000 index 000000000..3797ed99f --- /dev/null +++ b/renderer/macos/cinterop/include/appkit_resize.h @@ -0,0 +1 @@ +../../../desktop/c/appkit_resize.h \ No newline at end of file diff --git a/renderer/macos/cinterop/resize.def b/renderer/macos/cinterop/resize.def new file mode 100644 index 000000000..2955e15cb --- /dev/null +++ b/renderer/macos/cinterop/resize.def @@ -0,0 +1,6 @@ +# The resize accounting the AppKit window counts with, header only and without any window system +# in it, so that the Kotlin/Native window counts a drag exactly as the native image windows do. +# `include/appkit_resize.h` is a link to the one copy in `desktop/c/`. +package = resize + +headers = appkit_resize.h diff --git a/renderer/macos/src/JvmCompatShims.kt b/renderer/macos/src/JvmCompatShims.kt index a977b0592..290dd7fdd 100644 --- a/renderer/macos/src/JvmCompatShims.kt +++ b/renderer/macos/src/JvmCompatShims.kt @@ -32,6 +32,14 @@ object System { fun getenv(name: String): String? = platform.posix.getenv(name)?.toKString() + private val origin = kotlin.time.TimeSource.Monotonic.markNow() + + /** + * The monotonic clock in nanoseconds from when this was first asked, which is all the JVM's + * call of this name promises: only the difference between two answers means anything. + */ + fun nanoTime(): Long = origin.elapsedNow().inWholeNanoseconds + class ErrorStream internal constructor() { fun println(line: String) = NSLog("%s", line) } diff --git a/renderer/macos/src/MacosRenderer.kt b/renderer/macos/src/MacosRenderer.kt index 1600892f7..35bef5824 100644 --- a/renderer/macos/src/MacosRenderer.kt +++ b/renderer/macos/src/MacosRenderer.kt @@ -102,6 +102,8 @@ internal fun runRenderer(connection: () -> HostConnection): Int { cornerRadius = dressing.windowCornerRadius, ) window.setContent { ComposeRustContent(host, caption = window.caption.value) } + // The made-up input of a parity run, when `DXC_SYNTH` asks for it. + window.startSynthetic() application.activateIgnoringOtherApps(true) application.run() diff --git a/renderer/macos/src/MacosWindow.kt b/renderer/macos/src/MacosWindow.kt index 6456b3bf5..fc407a90c 100644 --- a/renderer/macos/src/MacosWindow.kt +++ b/renderer/macos/src/MacosWindow.kt @@ -23,8 +23,24 @@ import androidx.compose.ui.platform.WindowInfo import androidx.compose.ui.scene.CanvasLayersComposeScene import androidx.compose.ui.unit.Density import androidx.compose.ui.unit.IntSize +import java.lang.System import kotlinx.cinterop.CValue +import kotlinx.cinterop.alloc +import kotlinx.cinterop.nativeHeap +import kotlinx.cinterop.ptr import kotlinx.cinterop.useContents +import platform.AppKit.NSApplication +import platform.Foundation.NSDate +import platform.Foundation.NSRunLoop +import platform.Foundation.NSTimer +import platform.Foundation.NSMakeSize +import platform.QuartzCore.CATransaction +import resize.dxc_resize_present_against +import resize.dxc_resize_reset +import resize.dxc_resize_stale_ratio +import resize.dxc_resize_step +import resize.dxc_resize_stats +import resize.dxc_resize_stretched import platform.CoreGraphics.CGPoint import platform.CoreGraphics.CGRect import platform.Foundation.NSPointInRect @@ -156,7 +172,20 @@ internal class MacosWindow( // What the window says about itself, kept in step with the scene by the listener the // context carries. Pushed on change rather than asked for. - private val semantics = NativeSemantics { elements -> describeToReader(elements) } + private val semantics = NativeSemantics { elements -> + synthetic?.noteElements(elements) + describeToReader(elements) + } + + /** Made-up input and resizing, for the parity check. Null unless `DXC_SYNTH` asked. */ + private var synthetic: SyntheticInput? = null + + /** + * What a resize cost and what it showed, counted by the header the native image windows + * count with, so a drag is counted the same way on every path. Reported at the end of a + * synthetic run when `DXC_REPORT_RESIZE` is set. + */ + private val resizeStats = nativeHeap.alloc().also { dxc_resize_reset(it.ptr) } /** Where committed and composing text goes. */ private val textInput = NativeTextInput() @@ -218,10 +247,22 @@ internal class MacosWindow( val size = IntSize(widthInPixels, heightInPixels) measured = size scene.size = size + val begun = System.nanoTime() scene.render( canvas.asComposeCanvas(), (NSProcessInfo.processInfo.systemUptime * 1_000_000_000.0).toLong(), ) + if (synthetic != null) { + LatencyTrace.frameDrawn(System.nanoTime() - begun) + // Against the size the view has now, asked of it rather than remembered. + val scale = window.backingScaleFactor + view.frame.useContents { + dxc_resize_present_against( + resizeStats.ptr, widthInPixels, heightInPixels, + (this.size.width * scale).toInt(), (this.size.height * scale).toInt(), + ) + } + } // After the drawing, because that is when what is in the window has been placed // and can say where it is. Asked before, every control answers with an empty // rectangle and a reader finds the screen stacked in one corner. @@ -377,6 +418,12 @@ internal class MacosWindow( private val backdrop = object : NSVisualEffectView(window.frame) { override fun setFrameSize(newSize: CValue) { super.setFrameSize(newSize) + if (synthetic != null) { + val scale = window?.backingScaleFactor ?: 1.0 + newSize.useContents { + dxc_resize_step(resizeStats.ptr, (width * scale).toInt(), (height * scale).toInt()) + } + } measureCaption() } }.also { @@ -660,6 +707,71 @@ internal class MacosWindow( components.lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_RESUME) } + /** + * Starts the made-up input of a parity run: a click in the first text field, five letters, + * the sizes a drag would pass through, and then the end of the run. Asked for with + * `DXC_SYNTH`, the same variable and the same timeline every other window reads, so the + * numbers it leaves in `LatencyTrace` are comparable. + * + * Driven from a timer on the main run loop, which is where AppKit delivers everything + * else, so the made-up input arrives the way a real one would. + */ + fun startSynthetic() { + val what = System.getenv("DXC_SYNTH") ?: return + val input = SyntheticInput(what) + synthetic = input + // Something has to be asking for the tree, or the field is never found: a reader + // normally does, and nobody is reading this. + readerIsListening = true + NSTimer.scheduledTimerWithTimeInterval(SYNTHETIC_TURN_SECONDS, repeats = true) { _ -> + val now = System.nanoTime() + for (event in input.due(now, measured)) { + if (event.kind == WindowEvent.TEXT_COMMIT) LatencyTrace.inputSent() + LatencyTrace.mark("synthetic ${event.kind} sent") + scene.receive(event) + textInput.receive(event) + view.needsDisplay = true + } + if (input.resizeDue(now)) scriptedResize() + if (input.exitDue(now)) endSyntheticRun() + } + } + + /** + * Takes the window through the sizes a drag would, from the size it has to 360 by 420 in + * sixty steps, each one set and its drawing committed before the next. + */ + private fun scriptedResize() { + LatencyTrace.phase = "resize" + dxc_resize_reset(resizeStats.ptr) + val from = view.frame.useContents { size.width to size.height } + for (step in 1..SYNTHETIC_RESIZE_STEPS) { + val t = step.toDouble() / SYNTHETIC_RESIZE_STEPS + window.setContentSize( + NSMakeSize( + from.first + (SYNTHETIC_RESIZE_WIDTH - from.first) * t, + from.second + (SYNTHETIC_RESIZE_HEIGHT - from.second) * t, + ), + ) + CATransaction.flush() + NSRunLoop.currentRunLoop.runUntilDate(NSDate.dateWithTimeIntervalSinceNow(0.008)) + } + LatencyTrace.phase = "idle" + } + + /** Prints what the run measured and ends the application. */ + private fun endSyntheticRun() { + LatencyTrace.summary() + if (System.getenv("DXC_REPORT_RESIZE") != null) { + System.err.println( + "dxc resize: steps=${resizeStats.steps} presented=${resizeStats.presented} " + + "stale=${resizeStats.stale} stretched=${dxc_resize_stretched(resizeStats.ptr)} " + + "stale_ratio=${fixed(dxc_resize_stale_ratio(resizeStats.ptr), 4)}", + ) + } + NSApplication.sharedApplication().terminate(null) + } + /** * Hands the reader what the scene last said, as elements it can ask about. * @@ -804,3 +916,9 @@ private class MenuShortcut(private val run: () -> Unit) : platform.darwin.NSObje @kotlinx.cinterop.ObjCAction fun perform() = run() } + +/** How often the made-up input of a parity run looks at the clock. */ +private const val SYNTHETIC_TURN_SECONDS = 0.016 +private const val SYNTHETIC_RESIZE_STEPS = 60 +private const val SYNTHETIC_RESIZE_WIDTH = 360.0 +private const val SYNTHETIC_RESIZE_HEIGHT = 420.0 diff --git a/renderer/macos/src/shared/LatencyTrace.kt b/renderer/macos/src/shared/LatencyTrace.kt new file mode 120000 index 000000000..fe2933801 --- /dev/null +++ b/renderer/macos/src/shared/LatencyTrace.kt @@ -0,0 +1 @@ +../../../desktop/src/LatencyTrace.kt \ No newline at end of file diff --git a/renderer/macos/src/shared/SyntheticInput.kt b/renderer/macos/src/shared/SyntheticInput.kt new file mode 120000 index 000000000..beb699088 --- /dev/null +++ b/renderer/macos/src/shared/SyntheticInput.kt @@ -0,0 +1 @@ +../../../desktop/src/SyntheticInput.kt \ No newline at end of file diff --git a/renderer/macos/src/shared/WindowEvents.kt b/renderer/macos/src/shared/WindowEvents.kt new file mode 120000 index 000000000..2649a646b --- /dev/null +++ b/renderer/macos/src/shared/WindowEvents.kt @@ -0,0 +1 @@ +../../../desktop/src/WindowEvents.kt \ No newline at end of file diff --git a/scripts/parity/common.sh b/scripts/parity/common.sh new file mode 100644 index 000000000..6b01c52f8 --- /dev/null +++ b/scripts/parity/common.sh @@ -0,0 +1,58 @@ +#!/usr/bin/env bash +# Sourced by scripts/parity/linux.sh and scripts/parity/macos.sh. What the two have in common is +# everything that does not touch a display server: how a window's end-of-run output becomes +# rows, and how the table is printed. A row is `nameresult`; a result that starts with FAIL +# is a failure. + +row() { printf '%s\t%s\n' "$1" "$2" >> "$table"; } + +# Rows from what the window printed on standard error ($err): the numbers LatencyTrace and the +# resize accounting leave, which every window that draws a scene prints in the same words. +report_measurements() { +value() { sed -nE "s/.*parity $1 ([^ ]+).*/\1/p" "$err" | tail -1; } +field() { sed -nE "s/.*parity $1 [^ ]+ .*$2=([^ ]+).*/\1/p" "$err" | tail -1; } +latency="$(value input_latency_avg_ms)" +resize="$(value frame_resize_avg_ms)" +idle="$(value frame_idle_avg_ms)" +row input_latency_avg_ms "${latency:-FAIL not reported}" +row frame_resize_avg_ms "${resize:-FAIL not reported}" +row frame_resize_worst_ms "$(field frame_resize_avg_ms worst_ms)" +row frame_idle_avg_ms "${idle:-FAIL not reported}" +resize_line="$(grep -E 'dxc resize: ' "$err" | tail -1)" +resize_field() { echo "$resize_line" | sed -nE "s/.*$1=([0-9]+).*/\1/p"; } +row resize_steps "$(resize_field steps || true)" +row resize_presented "$(resize_field presented || true)" +row resize_stale "$(resize_field stale || true)" +row resize_stretched "$(resize_field stretched || true)" +ratio="$(echo "$resize_line" | sed -nE 's/.*stale_ratio=([0-9.]+).*/\1/p')" +if [[ -z "$ratio" ]]; then + row resize_stale_ratio "FAIL not reported" +elif [[ "$ratio" == "0" || "$ratio" =~ ^0\.0*$ ]]; then + row resize_stale_ratio "ok $ratio" +else + row resize_stale_ratio "FAIL $ratio of the frames were drawn at a size other than the window's" +fi +row system_theme "$(sed -nE 's/.*parity system_theme ([a-z]+).*/\1/p' "$err" | tail -1)" + +} + +# Prints the table, adds it to the job summary, and fails only when PARITY_STRICT=1. +print_table() { + echo "== parity: $label" + column -t -s $'\t' "$table" 2>/dev/null || cat "$table" + if [[ -n "${GITHUB_STEP_SUMMARY:-}" ]]; then + { + echo "### Parity: $label" + echo + echo "| check | $label |" + echo "|---|---|" + while IFS=$'\t' read -r name result; do echo "| $name | $result |"; done < "$table" + echo + } >> "$GITHUB_STEP_SUMMARY" + fi + if grep -q $'\tFAIL' "$table"; then + echo "::warning::parity $label has failing rows" + [[ "${PARITY_STRICT:-}" == "1" ]] && exit 1 + fi + exit 0 +} diff --git a/scripts/parity/linux.sh b/scripts/parity/linux.sh new file mode 100755 index 000000000..540365999 --- /dev/null +++ b/scripts/parity/linux.sh @@ -0,0 +1,151 @@ +#!/usr/bin/env bash +# Usage: scripts/parity/linux.sh