From 132c9b6e698c7bf6e9c144ca4cb134ce7cab4f71 Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Fri, 23 Oct 2026 14:57:23 -0600 Subject: [PATCH 1/3] chore(winit-android): vendor winit-android 0.31.0-beta.3 unmodified --- patches/winit-android/Cargo.toml | 85 ++ patches/winit-android/LICENSE | 201 +++++ patches/winit-android/README.md | 79 ++ patches/winit-android/src/event_loop.rs | 1095 +++++++++++++++++++++++ patches/winit-android/src/keycodes.rs | 622 +++++++++++++ patches/winit-android/src/lib.rs | 166 ++++ 6 files changed, 2248 insertions(+) create mode 100644 patches/winit-android/Cargo.toml create mode 100644 patches/winit-android/LICENSE create mode 100644 patches/winit-android/README.md create mode 100644 patches/winit-android/src/event_loop.rs create mode 100644 patches/winit-android/src/keycodes.rs create mode 100644 patches/winit-android/src/lib.rs diff --git a/patches/winit-android/Cargo.toml b/patches/winit-android/Cargo.toml new file mode 100644 index 0000000000..0944741fcc --- /dev/null +++ b/patches/winit-android/Cargo.toml @@ -0,0 +1,85 @@ +# THIS FILE IS AUTOMATICALLY GENERATED BY CARGO +# +# When uploading crates to the registry Cargo will automatically +# "normalize" Cargo.toml files for maximal compatibility +# with all versions of Cargo and also rewrite `path` dependencies +# to registry (e.g., crates.io) dependencies. +# +# If you are reading this file be aware that the original Cargo.toml +# will likely look very different (and much more reasonable). +# See Cargo.toml.orig for the original contents. + +[package] +edition = "2024" +rust-version = "1.86" +name = "winit-android" +version = "0.31.0-beta.3" +build = false +autolib = false +autobins = false +autoexamples = false +autotests = false +autobenches = false +description = "Winit's Android backend" +documentation = "https://docs.rs/winit-android" +readme = "README.md" +license = "Apache-2.0" +repository = "https://github.com/rust-windowing/winit" + +[package.metadata.docs.rs] +features = [ + "serde", + "native-activity", +] +targets = ["aarch64-linux-android"] + +[features] +game-activity = ["android-activity/game-activity"] +native-activity = ["android-activity/native-activity"] +serde = [ + "dep:serde", + "bitflags/serde", + "smol_str/serde", + "dpi/serde", + "winit-core/serde", +] + +[lib] +name = "winit_android" +path = "src/lib.rs" + +[dependencies.bitflags] +version = "2" + +[dependencies.dpi] +version = "0.1.2" + +[dependencies.rwh_06] +version = "0.6" +features = ["std"] +package = "raw-window-handle" + +[dependencies.serde] +version = "1" +features = ["serde_derive"] +optional = true + +[dependencies.smol_str] +version = "0.3" + +[dependencies.tracing] +version = "0.1.40" +default-features = false + +[dependencies.winit-core] +version = "=0.31.0-beta.3" + +[dev-dependencies] + +[target.'cfg(target_os = "android")'.dependencies.android-activity] +version = "0.6.0" + +[target.'cfg(target_os = "android")'.dependencies.ndk] +version = "0.9.0" +features = ["rwh_06"] +default-features = false diff --git a/patches/winit-android/LICENSE b/patches/winit-android/LICENSE new file mode 100644 index 0000000000..ad410e1130 --- /dev/null +++ b/patches/winit-android/LICENSE @@ -0,0 +1,201 @@ +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "{}" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright {yyyy} {name of copyright owner} + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. \ No newline at end of file diff --git a/patches/winit-android/README.md b/patches/winit-android/README.md new file mode 100644 index 0000000000..affc454a4e --- /dev/null +++ b/patches/winit-android/README.md @@ -0,0 +1,79 @@ +# winit - Cross-platform window creation and management in Rust + +[![Crates.io](https://img.shields.io/crates/v/winit.svg)](https://crates.io/crates/winit) +[![Docs.rs](https://docs.rs/winit/badge.svg)](https://docs.rs/winit) +[![UNSTABLE docs](https://img.shields.io/github/actions/workflow/status/rust-windowing/winit/docs.yml?branch=master&label=UNSTABLE%20docs +)](https://rust-windowing.github.io/winit/winit/index.html) +[![CI Status](https://github.com/rust-windowing/winit/workflows/CI/badge.svg)](https://github.com/rust-windowing/winit/actions) + +```toml +[dependencies] +winit = "0.31.0-beta.3" +``` + +## [Documentation](https://docs.rs/winit) + +For features _within_ the scope of winit, see [FEATURES.md](FEATURES.md). + +For features _outside_ the scope of winit, see [Are we GUI Yet?](https://areweguiyet.com/) and [Are we game yet?](https://arewegameyet.rs/), depending on what kind of project you're looking to do. + +## Contact Us + +Join us in our [![Matrix](https://img.shields.io/badge/Matrix-%23rust--windowing%3Amatrix.org-blueviolet.svg)](https://matrix.to/#/#rust-windowing:matrix.org) room. + +The maintainers have a meeting every friday at UTC 15. The meeting notes can be found [here](https://hackmd.io/@winit-meetings). + +## Usage + +Winit is a window creation and management library. It can create windows and lets you handle +events (for example: the window being resized, a key being pressed, a mouse movement, etc.) +produced by the window. + +Winit is designed to be a low-level brick in a hierarchy of libraries. Consequently, in order to +show something on the window you need to use the platform-specific getters provided by winit, or +another library. + +## CONTRIBUTING + +For contributing guidelines see [CONTRIBUTING.md](./CONTRIBUTING.md). + +## MSRV Policy + +This crate's Minimum Supported Rust Version (MSRV) is **1.86**. Changes to +the MSRV will be accompanied by a minor version bump. + +As a **tentative** policy, the upper bound of the MSRV is given by the following +formula: + +``` +min(sid, stable - 3) +``` + +Where `sid` is the current version of `rustc` provided by [Debian Sid], and +`stable` is the latest stable version of Rust. This bound may be broken in case of a major ecosystem shift or a security vulnerability. + +[Debian Sid]: https://packages.debian.org/sid/rustc + +An exception is made for the Android platform, where a higher Rust version +must be used for certain Android features. In this case, the MSRV will be +capped at the latest stable version of Rust minus three. This inconsistency is +not reflected in Cargo metadata, as it is not powerful enough to expose this +restriction. + +Redox OS is also not covered by this MSRV policy, as it requires a Rust nightly +toolchain to compile. + +All crates in the [`rust-windowing`] organizations have the +same MSRV policy. + +[`rust-windowing`]: https://github.com/rust-windowing + +### Platform-specific usage + +Check out the [`winit::platform`](https://docs.rs/winit/latest/winit/platform/index.html) module for platform-specific usage. + +### Repository License + +Note that the license in `LICENSE` doesn't apply in full to the DPI package [./dpi](./dpi). +Full details can be found in that folder's README. + diff --git a/patches/winit-android/src/event_loop.rs b/patches/winit-android/src/event_loop.rs new file mode 100644 index 0000000000..ec8abe4d96 --- /dev/null +++ b/patches/winit-android/src/event_loop.rs @@ -0,0 +1,1095 @@ +use std::cell::Cell; +use std::fmt; +use std::hash::Hash; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Arc, Mutex}; +use std::time::{Duration, Instant}; + +use android_activity::input::{InputEvent, KeyAction, Keycode, MotionAction}; +use android_activity::{ + AndroidApp, AndroidAppWaker, ConfigurationRef, InputStatus, MainEvent, Rect, +}; +use dpi::{PhysicalInsets, PhysicalPosition, PhysicalSize, Position, Size}; +use tracing::{debug, trace, warn}; +use winit_core::application::ApplicationHandler; +use winit_core::cursor::{Cursor, CustomCursor, CustomCursorSource}; +use winit_core::error::{EventLoopError, NotSupportedError, RequestError}; +use winit_core::event::{self, DeviceId, FingerId, Force, StartCause, SurfaceSizeWriter}; +use winit_core::event_loop::pump_events::PumpStatus; +use winit_core::event_loop::{ + ActiveEventLoop as RootActiveEventLoop, ControlFlow, DeviceEvents, EventLoopProvider, + EventLoopProxy as CoreEventLoopProxy, EventLoopProxyProvider, + OwnedDisplayHandle as CoreOwnedDisplayHandle, +}; +use winit_core::monitor::{Fullscreen, MonitorHandle as CoreMonitorHandle}; +use winit_core::window::{ + self, CursorGrabMode, ImeCapabilities, ImePurpose, ImeRequest, ImeRequestError, + ResizeDirection, Theme, Window as CoreWindow, WindowAttributes, WindowButtons, WindowId, + WindowLevel, +}; + +use crate::keycodes; + +static HAS_FOCUS: AtomicBool = AtomicBool::new(true); + +/// Returns the minimum `Option`, taking into account that `None` +/// equates to an infinite timeout, not a zero timeout (so can't just use +/// `Option::min`) +fn min_timeout(a: Option, b: Option) -> Option { + a.map_or(b, |a_timeout| b.map_or(Some(a_timeout), |b_timeout| Some(a_timeout.min(b_timeout)))) +} + +#[derive(Clone, Debug)] +struct SharedFlagSetter { + flag: Arc, +} +impl SharedFlagSetter { + fn set(&self) -> bool { + self.flag.compare_exchange(false, true, Ordering::AcqRel, Ordering::Relaxed).is_ok() + } +} + +#[derive(Debug)] +struct SharedFlag { + flag: Arc, +} + +// Used for queuing redraws from arbitrary threads. We don't care how many +// times a redraw is requested (so don't actually need to queue any data, +// we just need to know at the start of a main loop iteration if a redraw +// was queued and be able to read and clear the state atomically) +impl SharedFlag { + fn new() -> Self { + Self { flag: Arc::new(AtomicBool::new(false)) } + } + + fn setter(&self) -> SharedFlagSetter { + SharedFlagSetter { flag: self.flag.clone() } + } + + fn get_and_reset(&self) -> bool { + self.flag.swap(false, std::sync::atomic::Ordering::AcqRel) + } +} + +#[derive(Clone)] +struct RedrawRequester { + flag: SharedFlagSetter, + waker: AndroidAppWaker, +} + +impl fmt::Debug for RedrawRequester { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("RedrawRequester").field("flag", &self.flag).finish_non_exhaustive() + } +} + +impl RedrawRequester { + fn new(flag: &SharedFlag, waker: AndroidAppWaker) -> Self { + RedrawRequester { flag: flag.setter(), waker } + } + + fn request_redraw(&self) { + if self.flag.set() { + // Only explicitly try to wake up the main loop when the flag + // value changes + self.waker.wake(); + } + } +} + +#[derive(Debug)] +pub struct EventLoop { + pub android_app: AndroidApp, + window_target: ActiveEventLoop, + redraw_flag: SharedFlag, + loop_running: bool, // Dispatched `NewEvents` + running: bool, + pending_redraw: bool, + cause: StartCause, + primary_pointer: Option, + ignore_volume_keys: bool, + combining_accent: Option, +} + +#[derive(Debug, Clone, PartialEq, Eq, Hash)] +pub struct PlatformSpecificEventLoopAttributes { + pub android_app: Option, + pub ignore_volume_keys: bool, +} + +impl Default for PlatformSpecificEventLoopAttributes { + fn default() -> Self { + Self { android_app: Default::default(), ignore_volume_keys: true } + } +} + +// Android currently only supports one window +const GLOBAL_WINDOW: WindowId = WindowId::from_raw(0); + +impl EventLoop { + pub fn new(attributes: &PlatformSpecificEventLoopAttributes) -> Result { + static EVENT_LOOP_CREATED: AtomicBool = AtomicBool::new(false); + if EVENT_LOOP_CREATED.swap(true, Ordering::Relaxed) { + // For better cross-platformness. + return Err(EventLoopError::RecreationAttempt); + } + + let android_app = attributes.android_app.as_ref().expect( + "An `AndroidApp` as passed to android_main() is required to create an `EventLoop` on \ + Android", + ); + + let event_loop_proxy = Arc::new(EventLoopProxy::new(android_app.create_waker())); + + let redraw_flag = SharedFlag::new(); + + Ok(Self { + android_app: android_app.clone(), + primary_pointer: None, + window_target: ActiveEventLoop { + app: android_app.clone(), + control_flow: Cell::new(ControlFlow::default()), + exit: Cell::new(false), + redraw_requester: RedrawRequester::new(&redraw_flag, android_app.create_waker()), + event_loop_proxy, + }, + redraw_flag, + loop_running: false, + running: false, + pending_redraw: false, + cause: StartCause::Init, + ignore_volume_keys: attributes.ignore_volume_keys, + combining_accent: None, + }) + } + + pub fn window_target(&self) -> &dyn RootActiveEventLoop { + &self.window_target + } + + fn single_iteration( + &mut self, + main_event: Option>, + app: &mut A, + ) { + trace!("Mainloop iteration"); + + let cause = self.cause; + let mut pending_redraw = self.pending_redraw; + let mut resized = false; + + app.new_events(&self.window_target, cause); + + if let Some(event) = main_event { + trace!("Handling main event {:?}", event); + + match event { + MainEvent::InitWindow { .. } => { + app.can_create_surfaces(&self.window_target); + }, + MainEvent::TerminateWindow { .. } => { + app.destroy_surfaces(&self.window_target); + }, + MainEvent::WindowResized { .. } => resized = true, + MainEvent::RedrawNeeded { .. } => pending_redraw = true, + MainEvent::ContentRectChanged { .. } => { + warn!("TODO: find a way to notify application of content rect change"); + }, + MainEvent::GainedFocus => { + HAS_FOCUS.store(true, Ordering::Relaxed); + let event = event::WindowEvent::Focused(true); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + }, + MainEvent::LostFocus => { + HAS_FOCUS.store(false, Ordering::Relaxed); + let event = event::WindowEvent::Focused(false); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + }, + MainEvent::ConfigChanged { .. } => { + let old_scale_factor = scale_factor(&self.android_app); + let scale_factor = scale_factor(&self.android_app); + if (scale_factor - old_scale_factor).abs() < f64::EPSILON { + let new_surface_size = Arc::new(Mutex::new(screen_size(&self.android_app))); + let event = event::WindowEvent::ScaleFactorChanged { + surface_size_writer: SurfaceSizeWriter::new(Arc::downgrade( + &new_surface_size, + )), + scale_factor, + }; + + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + } + }, + MainEvent::LowMemory => { + app.memory_warning(&self.window_target); + }, + MainEvent::Start => { + app.resumed(self.window_target()); + }, + MainEvent::Resume { .. } => { + debug!("App Resumed - is running"); + // TODO: This is incorrect - will be solved in https://github.com/rust-windowing/winit/pull/3897 + self.running = true; + }, + MainEvent::SaveState { .. } => { + // XXX: how to forward this state to applications? + // XXX: also how do we expose state restoration to apps? + warn!("TODO: forward saveState notification to application"); + }, + MainEvent::Pause => { + debug!("App Paused - stopped running"); + // TODO: This is incorrect - will be solved in https://github.com/rust-windowing/winit/pull/3897 + self.running = false; + }, + MainEvent::Stop => { + app.suspended(self.window_target()); + }, + MainEvent::Destroy => { + // XXX: maybe exit mainloop to drop things before being + // killed by the OS? + warn!("TODO: forward onDestroy notification to application"); + }, + MainEvent::InsetsChanged { .. } => { + // XXX: how to forward this state to applications? + warn!("TODO: handle Android InsetsChanged notification"); + }, + unknown => { + trace!("Unknown MainEvent {unknown:?} (ignored)"); + }, + } + } else { + trace!("No main event to handle"); + } + + // temporarily decouple `android_app` from `self` so we aren't holding + // a borrow of `self` while iterating + let android_app = self.android_app.clone(); + + // Process input events + match android_app.input_events_iter() { + Ok(mut input_iter) => loop { + let read_event = + input_iter.next(|event| self.handle_input_event(&android_app, event, app)); + + if !read_event { + break; + } + }, + Err(err) => { + tracing::warn!("Failed to get input events iterator: {err:?}"); + }, + } + + if self.window_target.event_loop_proxy.wake_up.swap(false, Ordering::Relaxed) { + app.proxy_wake_up(&self.window_target); + } + + if self.running { + if resized { + let size = if let Some(native_window) = self.android_app.native_window().as_ref() { + let width = native_window.width() as _; + let height = native_window.height() as _; + PhysicalSize::new(width, height) + } else { + PhysicalSize::new(0, 0) + }; + let event = event::WindowEvent::SurfaceResized(size); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + } + + pending_redraw |= self.redraw_flag.get_and_reset(); + if pending_redraw { + pending_redraw = false; + let event = event::WindowEvent::RedrawRequested; + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + } + } + + // This is always the last event we dispatch before poll again + app.about_to_wait(&self.window_target); + + self.pending_redraw = pending_redraw; + } + + fn handle_input_event( + &mut self, + android_app: &AndroidApp, + event: &InputEvent<'_>, + app: &mut A, + ) -> InputStatus { + let mut input_status = InputStatus::Handled; + match event { + InputEvent::MotionEvent(motion_event) => { + let device_id = Some(DeviceId::from_raw(motion_event.device_id() as i64)); + let action = motion_event.action(); + + let pointers: Option< + Box>>, + > = match action { + MotionAction::Down + | MotionAction::PointerDown + | MotionAction::Up + | MotionAction::PointerUp => Some(Box::new(std::iter::once( + motion_event.pointer_at_index(motion_event.pointer_index()), + ))), + MotionAction::Move | MotionAction::Cancel => { + Some(Box::new(motion_event.pointers())) + }, + // TODO mouse events + _ => None, + }; + + for pointer in pointers.into_iter().flatten() { + let tool_type = pointer.tool_type(); + let position = PhysicalPosition { x: pointer.x() as _, y: pointer.y() as _ }; + trace!( + "Input event {device_id:?}, {action:?}, loc={position:?}, \ + pointer={pointer:?}, tool_type={tool_type:?}" + ); + let finger_id = FingerId::from_raw(pointer.pointer_id() as usize); + let force = Some(Force::Normalized(pointer.pressure() as f64)); + + match action { + MotionAction::Down | MotionAction::PointerDown => { + let primary = action == MotionAction::Down; + if primary { + self.primary_pointer = Some(finger_id); + } + let event = event::WindowEvent::PointerEntered { + device_id, + primary, + position, + kind: match tool_type { + android_activity::input::ToolType::Finger => { + event::PointerKind::Touch(finger_id) + }, + // TODO mouse events + android_activity::input::ToolType::Mouse => continue, + _ => event::PointerKind::Unknown, + }, + }; + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + let event = event::WindowEvent::PointerButton { + device_id, + primary, + state: event::ElementState::Pressed, + position, + button: match tool_type { + android_activity::input::ToolType::Finger => { + event::ButtonSource::Touch { finger_id, force } + }, + // TODO mouse events + android_activity::input::ToolType::Mouse => continue, + _ => event::ButtonSource::Unknown(0), + }, + is_macos_activation_click: false, + }; + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + }, + MotionAction::Move => { + let primary = self.primary_pointer == Some(finger_id); + let event = event::WindowEvent::PointerMoved { + device_id, + primary, + position, + source: match tool_type { + android_activity::input::ToolType::Finger => { + event::PointerSource::Touch { finger_id, force } + }, + // TODO mouse events + android_activity::input::ToolType::Mouse => continue, + _ => event::PointerSource::Unknown, + }, + }; + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + }, + MotionAction::Up | MotionAction::PointerUp | MotionAction::Cancel => { + let primary = action == MotionAction::Up + || (action == MotionAction::Cancel + && self.primary_pointer == Some(finger_id)); + + if primary { + self.primary_pointer = None; + } + + if let MotionAction::Up | MotionAction::PointerUp = action { + let event = event::WindowEvent::PointerButton { + device_id, + primary, + state: event::ElementState::Released, + position, + button: match tool_type { + android_activity::input::ToolType::Finger => { + event::ButtonSource::Touch { finger_id, force } + }, + // TODO mouse events + android_activity::input::ToolType::Mouse => continue, + _ => event::ButtonSource::Unknown(0), + }, + is_macos_activation_click: false, + }; + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + } + + let event = event::WindowEvent::PointerLeft { + device_id, + primary, + position: Some(position), + kind: match tool_type { + android_activity::input::ToolType::Finger => { + event::PointerKind::Touch(finger_id) + }, + // TODO mouse events + android_activity::input::ToolType::Mouse => continue, + _ => event::PointerKind::Unknown, + }, + }; + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + }, + _ => unreachable!(), + } + } + }, + InputEvent::KeyEvent(key) => { + match key.key_code() { + // Flag keys related to volume as unhandled. While winit does not have a way for + // applications to configure what keys to flag as handled, + // this appears to be a good default until winit + // can be configured. + Keycode::VolumeUp | Keycode::VolumeDown | Keycode::VolumeMute + if self.ignore_volume_keys => + { + input_status = InputStatus::Unhandled + }, + keycode => { + let state = match key.action() { + KeyAction::Down => event::ElementState::Pressed, + KeyAction::Up => event::ElementState::Released, + _ => event::ElementState::Released, + }; + + let key_char = keycodes::character_map_and_combine_key( + android_app, + key, + &mut self.combining_accent, + ); + + let logical_key = keycodes::to_logical(key_char, keycode); + let text = if state == event::ElementState::Pressed { + logical_key.to_text().map(smol_str::SmolStr::new) + } else { + None + }; + + let event = event::WindowEvent::KeyboardInput { + device_id: Some(DeviceId::from_raw(key.device_id() as i64)), + event: event::KeyEvent { + state, + physical_key: keycodes::to_physical_key(keycode), + logical_key, + location: keycodes::to_location(keycode), + repeat: key.repeat_count() > 0, + text: text.clone(), + text_with_all_modifiers: text, + key_without_modifiers: keycodes::to_logical(key_char, keycode), + }, + is_synthetic: false, + }; + + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + }, + } + }, + _ => { + warn!("Unknown android_activity input event {event:?}") + }, + } + + input_status + } + + pub fn run_app_on_demand( + &mut self, + mut app: A, + ) -> Result<(), EventLoopError> { + self.window_target.clear_exit(); + loop { + match self.pump_app_events(None, &mut app) { + PumpStatus::Exit(0) => { + break Ok(()); + }, + PumpStatus::Exit(code) => { + break Err(EventLoopError::ExitFailure(code)); + }, + _ => { + continue; + }, + } + } + } + + pub fn pump_app_events( + &mut self, + timeout: Option, + mut app: A, + ) -> PumpStatus { + if !self.loop_running { + self.loop_running = true; + + // Reset the internal state for the loop as we start running to + // ensure consistent behaviour in case the loop runs and exits more + // than once + self.pending_redraw = false; + self.cause = StartCause::Init; + + // run the initial loop iteration + self.single_iteration(None, &mut app); + } + + // Consider the possibility that the `StartCause::Init` iteration could + // request to Exit + if !self.exiting() { + self.poll_events_with_timeout(timeout, &mut app); + } + if self.exiting() { + self.loop_running = false; + + PumpStatus::Exit(0) + } else { + PumpStatus::Continue + } + } + + fn poll_events_with_timeout( + &mut self, + mut timeout: Option, + app: &mut A, + ) { + let start = Instant::now(); + + self.pending_redraw |= self.redraw_flag.get_and_reset(); + + timeout = if self.running + && (self.pending_redraw + || self.window_target.event_loop_proxy.wake_up.load(Ordering::Relaxed)) + { + // If we already have work to do then we don't want to block on the next poll + Some(Duration::ZERO) + } else { + let control_flow_timeout = match self.control_flow() { + ControlFlow::Wait => None, + ControlFlow::Poll => Some(Duration::ZERO), + ControlFlow::WaitUntil(wait_deadline) => { + Some(wait_deadline.saturating_duration_since(start)) + }, + }; + + min_timeout(control_flow_timeout, timeout) + }; + + let android_app = self.android_app.clone(); // Don't borrow self as part of poll expression + android_app.poll_events(timeout, |poll_event| { + let mut main_event = None; + + match poll_event { + android_activity::PollEvent::Wake => { + // In the X11 backend it's noted that too many false-positive wake ups + // would cause the event loop to run continuously. They handle this by + // re-checking for pending events (assuming they cover all + // valid reasons for a wake up). + // + // For now, user_events and redraw_requests are the only reasons to expect + // a wake up here so we can ignore the wake up if there are no events/requests. + // We also ignore wake ups while suspended. + self.pending_redraw |= self.redraw_flag.get_and_reset(); + if !self.running + || (!self.pending_redraw + && !self.window_target.event_loop_proxy.wake_up.load(Ordering::Relaxed)) + { + return; + } + }, + android_activity::PollEvent::Timeout => {}, + android_activity::PollEvent::Main(event) => { + main_event = Some(event); + }, + unknown_event => { + warn!("Unknown poll event {unknown_event:?} (ignored)"); + }, + } + + self.cause = match self.control_flow() { + ControlFlow::Poll => StartCause::Poll, + ControlFlow::Wait => StartCause::WaitCancelled { start, requested_resume: None }, + ControlFlow::WaitUntil(deadline) => { + if Instant::now() < deadline { + StartCause::WaitCancelled { start, requested_resume: Some(deadline) } + } else { + StartCause::ResumeTimeReached { start, requested_resume: deadline } + } + }, + }; + + self.single_iteration(main_event, app); + }); + } + + fn control_flow(&self) -> ControlFlow { + self.window_target.control_flow() + } + + fn exiting(&self) -> bool { + self.window_target.exiting() + } +} + +impl EventLoopProvider for EventLoop { + fn run_app( + mut self, + mut app: A, + ) -> Result<(), EventLoopError> { + let result = self.run_app_on_demand(&mut app); + // SAFETY: unsure that the state is dropped before the exit from the event loop. + drop(app); + result + } + + fn create_proxy(&self) -> CoreEventLoopProxy { + self.window_target().create_proxy() + } + + fn owned_display_handle(&self) -> CoreOwnedDisplayHandle { + self.window_target().owned_display_handle() + } + + fn listen_device_events(&self, allowed: DeviceEvents) { + self.window_target().listen_device_events(allowed); + } + + fn set_control_flow(&self, control_flow: ControlFlow) { + self.window_target().set_control_flow(control_flow); + } + + fn create_custom_cursor( + &self, + custom_cursor: CustomCursorSource, + ) -> Result { + self.window_target().create_custom_cursor(custom_cursor) + } +} + +pub struct EventLoopProxy { + wake_up: AtomicBool, + waker: AndroidAppWaker, +} + +impl fmt::Debug for EventLoopProxy { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("EventLoopProxy").field("wake_up", &self.wake_up).finish_non_exhaustive() + } +} + +impl EventLoopProxy { + fn new(waker: AndroidAppWaker) -> Self { + Self { wake_up: AtomicBool::new(false), waker } + } +} + +impl EventLoopProxyProvider for EventLoopProxy { + fn wake_up(&self) { + self.wake_up.store(true, Ordering::Relaxed); + self.waker.wake(); + } +} + +#[derive(Debug)] +pub struct ActiveEventLoop { + pub(crate) app: AndroidApp, + control_flow: Cell, + exit: Cell, + redraw_requester: RedrawRequester, + event_loop_proxy: Arc, +} + +impl ActiveEventLoop { + fn clear_exit(&self) { + self.exit.set(false); + } +} + +impl RootActiveEventLoop for ActiveEventLoop { + fn create_proxy(&self) -> CoreEventLoopProxy { + CoreEventLoopProxy::new(self.event_loop_proxy.clone()) + } + + fn create_window( + &self, + window_attributes: WindowAttributes, + ) -> Result, RequestError> { + Ok(Box::new(Window::new(self, window_attributes)?)) + } + + fn create_custom_cursor( + &self, + _source: CustomCursorSource, + ) -> Result { + Err(NotSupportedError::new("create_custom_cursor is not supported").into()) + } + + fn available_monitors(&self) -> Box> { + Box::new(std::iter::empty()) + } + + fn primary_monitor(&self) -> Option { + None + } + + fn system_theme(&self) -> Option { + None + } + + fn listen_device_events(&self, _allowed: DeviceEvents) {} + + fn set_control_flow(&self, control_flow: ControlFlow) { + self.control_flow.set(control_flow) + } + + fn control_flow(&self) -> ControlFlow { + self.control_flow.get() + } + + fn exit(&self) { + self.exit.set(true) + } + + fn exiting(&self) -> bool { + self.exit.get() + } + + fn owned_display_handle(&self) -> CoreOwnedDisplayHandle { + CoreOwnedDisplayHandle::new(Arc::new(OwnedDisplayHandle)) + } + + fn rwh_06_handle(&self) -> &dyn rwh_06::HasDisplayHandle { + self + } +} + +impl rwh_06::HasDisplayHandle for ActiveEventLoop { + fn display_handle(&self) -> Result, rwh_06::HandleError> { + let raw = rwh_06::AndroidDisplayHandle::new(); + Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw.into()) }) + } +} + +#[derive(Clone, PartialEq, Eq)] +pub(crate) struct OwnedDisplayHandle; + +impl rwh_06::HasDisplayHandle for OwnedDisplayHandle { + fn display_handle(&self) -> Result, rwh_06::HandleError> { + let raw = rwh_06::AndroidDisplayHandle::new(); + Ok(unsafe { rwh_06::DisplayHandle::borrow_raw(raw.into()) }) + } +} + +#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] +pub struct PlatformSpecificWindowAttributes; + +#[derive(Debug)] +pub struct Window { + app: AndroidApp, + ime_capabilities: Mutex>, + redraw_requester: RedrawRequester, +} + +impl Window { + pub(crate) fn new( + el: &ActiveEventLoop, + window_attrs: window::WindowAttributes, + ) -> Result { + if window_attrs.window_type() == window::WindowType::Popup { + return Err(RequestError::NotSupported(NotSupportedError::new( + "Popups are not implemented for Android", + ))); + } + + // FIXME this ignores the rest of the requested window attributes + + Ok(Self { + app: el.app.clone(), + ime_capabilities: Default::default(), + redraw_requester: el.redraw_requester.clone(), + }) + } + + pub(crate) fn config(&self) -> ConfigurationRef { + self.app.config() + } + + pub(crate) fn content_rect(&self) -> Rect { + self.app.content_rect() + } + + // Allow the usage of HasRawWindowHandle inside this function + #[allow(deprecated)] + fn raw_window_handle_rwh_06(&self) -> Result { + use rwh_06::HasRawWindowHandle; + + if let Some(native_window) = self.app.native_window().as_ref() { + native_window.raw_window_handle() + } else { + tracing::error!( + "Cannot get the native window, it's null and will always be null before \ + Event::Resumed and after Event::Suspended. Make sure you only call this function \ + between those events." + ); + Err(rwh_06::HandleError::Unavailable) + } + } + + fn raw_display_handle_rwh_06(&self) -> Result { + Ok(rwh_06::RawDisplayHandle::Android(rwh_06::AndroidDisplayHandle::new())) + } +} + +impl rwh_06::HasDisplayHandle for Window { + fn display_handle(&self) -> Result, rwh_06::HandleError> { + let raw = self.raw_display_handle_rwh_06()?; + unsafe { Ok(rwh_06::DisplayHandle::borrow_raw(raw)) } + } +} + +impl rwh_06::HasWindowHandle for Window { + fn window_handle(&self) -> Result, rwh_06::HandleError> { + let raw = self.raw_window_handle_rwh_06()?; + unsafe { Ok(rwh_06::WindowHandle::borrow_raw(raw)) } + } +} + +impl CoreWindow for Window { + fn window_type(&self) -> window::WindowType { + window::WindowType::Window + } + + fn id(&self) -> WindowId { + GLOBAL_WINDOW + } + + fn primary_monitor(&self) -> Option { + None + } + + fn available_monitors(&self) -> Box> { + Box::new(std::iter::empty()) + } + + fn current_monitor(&self) -> Option { + None + } + + fn scale_factor(&self) -> f64 { + scale_factor(&self.app) + } + + fn request_redraw(&self) { + self.redraw_requester.request_redraw() + } + + fn pre_present_notify(&self) {} + + fn surface_position(&self) -> PhysicalPosition { + (0, 0).into() + } + + fn outer_position(&self) -> Result, RequestError> { + Err(NotSupportedError::new("outer_position is not supported").into()) + } + + fn set_outer_position(&self, _position: Position) { + // no effect + } + + fn surface_size(&self) -> PhysicalSize { + self.outer_size() + } + + fn request_surface_size(&self, _size: Size) -> Option> { + Some(self.surface_size()) + } + + fn outer_size(&self) -> PhysicalSize { + screen_size(&self.app) + } + + fn safe_area(&self) -> PhysicalInsets { + PhysicalInsets::new(0, 0, 0, 0) + } + + fn set_min_surface_size(&self, _: Option) {} + + fn set_max_surface_size(&self, _: Option) {} + + fn surface_resize_increments(&self) -> Option> { + None + } + + fn set_surface_resize_increments(&self, _increments: Option) {} + + fn set_title(&self, _title: &str) {} + + fn set_transparent(&self, _transparent: bool) {} + + fn set_blur(&self, _blur: bool) {} + + fn set_visible(&self, _visibility: bool) {} + + fn is_visible(&self) -> Option { + None + } + + fn set_resizable(&self, _resizeable: bool) {} + + fn is_resizable(&self) -> bool { + false + } + + fn set_enabled_buttons(&self, _buttons: WindowButtons) {} + + fn enabled_buttons(&self) -> WindowButtons { + WindowButtons::all() + } + + fn set_minimized(&self, _minimized: bool) {} + + fn is_minimized(&self) -> Option { + None + } + + fn set_maximized(&self, _maximized: bool) {} + + fn is_maximized(&self) -> bool { + false + } + + fn set_fullscreen(&self, _monitor: Option) { + warn!("Cannot set fullscreen on Android"); + } + + fn fullscreen(&self) -> Option { + None + } + + fn set_decorations(&self, _decorations: bool) {} + + fn is_decorated(&self) -> bool { + true + } + + fn set_window_level(&self, _level: WindowLevel) {} + + fn set_window_icon(&self, _window_icon: Option) {} + + fn set_ime_cursor_area(&self, _position: Position, _size: Size) {} + + fn request_ime_update(&self, request: ImeRequest) -> Result<(), ImeRequestError> { + let mut current_caps = self.ime_capabilities.lock().unwrap(); + match request { + ImeRequest::Enable(enable) => { + let (capabilities, _) = enable.into_raw(); + if current_caps.is_some() { + return Err(ImeRequestError::AlreadyEnabled); + } + *current_caps = Some(capabilities); + self.app.show_soft_input(true); + }, + ImeRequest::Update(_) => { + if current_caps.is_none() { + return Err(ImeRequestError::NotEnabled); + } + }, + ImeRequest::Disable => { + *current_caps = None; + self.app.hide_soft_input(true); + }, + _ => return Err(ImeRequestError::NotSupported), + } + + Ok(()) + } + + fn ime_capabilities(&self) -> Option { + *self.ime_capabilities.lock().unwrap() + } + + fn set_ime_purpose(&self, _purpose: ImePurpose) {} + + fn focus_window(&self) {} + + fn request_user_attention(&self, _request_type: Option) {} + + fn set_cursor(&self, _: Cursor) {} + + fn set_cursor_position(&self, _: Position) -> Result<(), RequestError> { + Err(NotSupportedError::new("set_cursor_position is not supported").into()) + } + + fn set_cursor_grab(&self, _: CursorGrabMode) -> Result<(), RequestError> { + Err(NotSupportedError::new("set_cursor_grab is not supported").into()) + } + + fn set_cursor_visible(&self, _: bool) {} + + fn drag_window(&self) -> Result<(), RequestError> { + Err(NotSupportedError::new("drag_window is not supported").into()) + } + + fn drag_resize_window(&self, _direction: ResizeDirection) -> Result<(), RequestError> { + Err(NotSupportedError::new("drag_resize_window").into()) + } + + #[inline] + fn show_window_menu(&self, _position: Position) {} + + fn set_cursor_hittest(&self, _hittest: bool) -> Result<(), RequestError> { + Err(NotSupportedError::new("set_cursor_hittest is not supported").into()) + } + + fn set_theme(&self, _theme: Option) {} + + fn theme(&self) -> Option { + None + } + + fn set_content_protected(&self, _protected: bool) {} + + fn has_focus(&self) -> bool { + HAS_FOCUS.load(Ordering::Relaxed) + } + + fn title(&self) -> String { + String::new() + } + + fn reset_dead_keys(&self) {} + + fn rwh_06_display_handle(&self) -> &dyn rwh_06::HasDisplayHandle { + self + } + + fn rwh_06_window_handle(&self) -> &dyn rwh_06::HasWindowHandle { + self + } +} + +fn screen_size(app: &AndroidApp) -> PhysicalSize { + if let Some(native_window) = app.native_window() { + PhysicalSize::new(native_window.width() as _, native_window.height() as _) + } else { + PhysicalSize::new(0, 0) + } +} + +fn scale_factor(app: &AndroidApp) -> f64 { + app.config().density().map(|dpi| dpi as f64 / 160.0).unwrap_or(1.0) +} diff --git a/patches/winit-android/src/keycodes.rs b/patches/winit-android/src/keycodes.rs new file mode 100644 index 0000000000..9b1fe1f0df --- /dev/null +++ b/patches/winit-android/src/keycodes.rs @@ -0,0 +1,622 @@ +use android_activity::AndroidApp; +use android_activity::input::{KeyAction, KeyEvent, KeyMapChar, Keycode}; +use winit_core::keyboard::{ + Key, KeyCode, KeyLocation, NamedKey, NativeKey, NativeKeyCode, PhysicalKey, +}; + +pub fn to_physical_key(keycode: Keycode) -> PhysicalKey { + PhysicalKey::Code(match keycode { + Keycode::A => KeyCode::KeyA, + Keycode::B => KeyCode::KeyB, + Keycode::C => KeyCode::KeyC, + Keycode::D => KeyCode::KeyD, + Keycode::E => KeyCode::KeyE, + Keycode::F => KeyCode::KeyF, + Keycode::G => KeyCode::KeyG, + Keycode::H => KeyCode::KeyH, + Keycode::I => KeyCode::KeyI, + Keycode::J => KeyCode::KeyJ, + Keycode::K => KeyCode::KeyK, + Keycode::L => KeyCode::KeyL, + Keycode::M => KeyCode::KeyM, + Keycode::N => KeyCode::KeyN, + Keycode::O => KeyCode::KeyO, + Keycode::P => KeyCode::KeyP, + Keycode::Q => KeyCode::KeyQ, + Keycode::R => KeyCode::KeyR, + Keycode::S => KeyCode::KeyS, + Keycode::T => KeyCode::KeyT, + Keycode::U => KeyCode::KeyU, + Keycode::V => KeyCode::KeyV, + Keycode::W => KeyCode::KeyW, + Keycode::X => KeyCode::KeyX, + Keycode::Y => KeyCode::KeyY, + Keycode::Z => KeyCode::KeyZ, + + Keycode::Keycode0 => KeyCode::Digit0, + Keycode::Keycode1 => KeyCode::Digit1, + Keycode::Keycode2 => KeyCode::Digit2, + Keycode::Keycode3 => KeyCode::Digit3, + Keycode::Keycode4 => KeyCode::Digit4, + Keycode::Keycode5 => KeyCode::Digit5, + Keycode::Keycode6 => KeyCode::Digit6, + Keycode::Keycode7 => KeyCode::Digit7, + Keycode::Keycode8 => KeyCode::Digit8, + Keycode::Keycode9 => KeyCode::Digit9, + + Keycode::Numpad0 => KeyCode::Numpad0, + Keycode::Numpad1 => KeyCode::Numpad1, + Keycode::Numpad2 => KeyCode::Numpad2, + Keycode::Numpad3 => KeyCode::Numpad3, + Keycode::Numpad4 => KeyCode::Numpad4, + Keycode::Numpad5 => KeyCode::Numpad5, + Keycode::Numpad6 => KeyCode::Numpad6, + Keycode::Numpad7 => KeyCode::Numpad7, + Keycode::Numpad8 => KeyCode::Numpad8, + Keycode::Numpad9 => KeyCode::Numpad9, + + Keycode::NumpadAdd => KeyCode::NumpadAdd, + Keycode::NumpadSubtract => KeyCode::NumpadSubtract, + Keycode::NumpadMultiply => KeyCode::NumpadMultiply, + Keycode::NumpadDivide => KeyCode::NumpadDivide, + Keycode::NumpadEnter => KeyCode::NumpadEnter, + Keycode::NumpadEquals => KeyCode::NumpadEqual, + Keycode::NumpadComma => KeyCode::NumpadComma, + Keycode::NumpadDot => KeyCode::NumpadDecimal, + Keycode::NumLock => KeyCode::NumLock, + + Keycode::DpadLeft => KeyCode::ArrowLeft, + Keycode::DpadRight => KeyCode::ArrowRight, + Keycode::DpadUp => KeyCode::ArrowUp, + Keycode::DpadDown => KeyCode::ArrowDown, + + Keycode::F1 => KeyCode::F1, + Keycode::F2 => KeyCode::F2, + Keycode::F3 => KeyCode::F3, + Keycode::F4 => KeyCode::F4, + Keycode::F5 => KeyCode::F5, + Keycode::F6 => KeyCode::F6, + Keycode::F7 => KeyCode::F7, + Keycode::F8 => KeyCode::F8, + Keycode::F9 => KeyCode::F9, + Keycode::F10 => KeyCode::F10, + Keycode::F11 => KeyCode::F11, + Keycode::F12 => KeyCode::F12, + + Keycode::Space => KeyCode::Space, + Keycode::Escape => KeyCode::Escape, + Keycode::Enter => KeyCode::Enter, // not on the Numpad + Keycode::Tab => KeyCode::Tab, + + Keycode::PageUp => KeyCode::PageUp, + Keycode::PageDown => KeyCode::PageDown, + Keycode::MoveHome => KeyCode::Home, + Keycode::MoveEnd => KeyCode::End, + Keycode::Insert => KeyCode::Insert, + + Keycode::Del => KeyCode::Backspace, // Backspace (above Enter) + Keycode::ForwardDel => KeyCode::Delete, // Delete (below Insert) + + Keycode::Copy => KeyCode::Copy, + Keycode::Paste => KeyCode::Paste, + Keycode::Cut => KeyCode::Cut, + + Keycode::VolumeUp => KeyCode::AudioVolumeUp, + Keycode::VolumeDown => KeyCode::AudioVolumeDown, + Keycode::VolumeMute => KeyCode::AudioVolumeMute, + // Keycode::Mute => None, // Microphone mute + Keycode::MediaPlayPause => KeyCode::MediaPlayPause, + Keycode::MediaStop => KeyCode::MediaStop, + Keycode::MediaNext => KeyCode::MediaTrackNext, + Keycode::MediaPrevious => KeyCode::MediaTrackPrevious, + Keycode::MediaEject => KeyCode::Eject, + + Keycode::Plus => KeyCode::Equal, + Keycode::Minus => KeyCode::Minus, + // Winit doesn't differentiate both '+' and '=', considering they are usually + // on the same physical key + Keycode::Equals => KeyCode::Equal, + Keycode::Semicolon => KeyCode::Semicolon, + Keycode::Slash => KeyCode::Slash, + Keycode::Backslash => KeyCode::Backslash, + Keycode::Comma => KeyCode::Comma, + Keycode::Period => KeyCode::Period, + Keycode::Apostrophe => KeyCode::Quote, + Keycode::Grave => KeyCode::Backquote, + + // Winit doesn't expose a SysRq code, so map to PrintScreen since it's + // usually the same physical key + Keycode::Sysrq => KeyCode::PrintScreen, + // These are usually the same (Pause/Break) + Keycode::Break => KeyCode::Pause, + // These are exactly the same + Keycode::ScrollLock => KeyCode::ScrollLock, + + Keycode::Eisu => KeyCode::Lang2, + Keycode::Muhenkan => KeyCode::NonConvert, + Keycode::Henkan => KeyCode::Convert, + Keycode::Yen => KeyCode::IntlYen, + Keycode::Ro => KeyCode::IntlRo, + Keycode::Kana => KeyCode::Lang1, + Keycode::KatakanaHiragana => KeyCode::KanaMode, + + Keycode::CtrlLeft => KeyCode::ControlLeft, + Keycode::CtrlRight => KeyCode::ControlRight, + + Keycode::ShiftLeft => KeyCode::ShiftLeft, + Keycode::ShiftRight => KeyCode::ShiftRight, + + Keycode::AltLeft => KeyCode::AltLeft, + Keycode::AltRight => KeyCode::AltRight, + + Keycode::MetaLeft => KeyCode::MetaLeft, + Keycode::MetaRight => KeyCode::MetaRight, + + Keycode::LeftBracket => KeyCode::BracketLeft, + Keycode::RightBracket => KeyCode::BracketRight, + + Keycode::Power => KeyCode::Power, + Keycode::Sleep => KeyCode::Sleep, // what about SoftSleep? + Keycode::Wakeup => KeyCode::WakeUp, + + Keycode::CapsLock => KeyCode::CapsLock, + Keycode::Help => KeyCode::Help, + + Keycode::Back => KeyCode::BrowserBack, + Keycode::Forward => KeyCode::BrowserForward, + Keycode::Refresh => KeyCode::BrowserRefresh, + Keycode::Search => KeyCode::BrowserSearch, + + keycode => return PhysicalKey::Unidentified(NativeKeyCode::Android(keycode.into())), + }) +} + +/// Tries to map the `key_event` to a `KeyMapChar` containing a unicode character or dead key accent +/// +/// This takes a `KeyEvent` and looks up its corresponding `KeyCharacterMap` and +/// uses that to try and map the `key_code` + `meta_state` to a unicode +/// character or a dead key that can be combined with the next key press. +pub fn character_map_and_combine_key( + app: &AndroidApp, + key_event: &KeyEvent<'_>, + combining_accent: &mut Option, +) -> Option { + let device_id = key_event.device_id(); + + let key_map = match app.device_key_character_map(device_id) { + Ok(key_map) => key_map, + Err(err) => { + tracing::warn!("Failed to look up `KeyCharacterMap` for device {device_id}: {err:?}"); + return None; + }, + }; + + match key_map.get(key_event.key_code(), key_event.meta_state()) { + Ok(KeyMapChar::Unicode(unicode)) => { + // Only do dead key combining on key down + if key_event.action() == KeyAction::Down { + let combined_unicode = if let Some(accent) = combining_accent { + match key_map.get_dead_char(*accent, unicode) { + Ok(Some(key)) => Some(key), + Ok(None) => None, + Err(err) => { + tracing::warn!( + "KeyEvent: Failed to combine 'dead key' accent '{accent}' with \ + '{unicode}': {err:?}" + ); + None + }, + } + } else { + Some(unicode) + }; + *combining_accent = None; + combined_unicode.map(KeyMapChar::Unicode) + } else { + Some(KeyMapChar::Unicode(unicode)) + } + }, + Ok(KeyMapChar::CombiningAccent(accent)) => { + if key_event.action() == KeyAction::Down { + *combining_accent = Some(accent); + } + Some(KeyMapChar::CombiningAccent(accent)) + }, + Ok(KeyMapChar::None) => { + // Leave any combining_accent state in tact (seems to match how other + // Android apps work) + None + }, + Err(err) => { + tracing::warn!("KeyEvent: Failed to get key map character: {err:?}"); + *combining_accent = None; + None + }, + } +} + +pub fn to_logical(key_char: Option, keycode: Keycode) -> Key { + use android_activity::input::Keycode::*; + + let native = NativeKey::Android(keycode.into()); + + match key_char { + Some(KeyMapChar::Unicode(c)) => Key::Character(smol_str::SmolStr::from_iter([c])), + Some(KeyMapChar::CombiningAccent(c)) => Key::Dead(Some(c)), + None | Some(KeyMapChar::None) => match keycode { + // Using `BrowserHome` instead of `GoHome` according to + // https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent/key/Key_Values + Home => Key::Named(NamedKey::BrowserHome), + Back => Key::Named(NamedKey::BrowserBack), + Call => Key::Named(NamedKey::Call), + Endcall => Key::Named(NamedKey::EndCall), + + //------------------------------------------------------------------------------- + // These should be redundant because they should have already been matched + // as `KeyMapChar::Unicode`, but also matched here as a fallback + Keycode0 => Key::Character("0".into()), + Keycode1 => Key::Character("1".into()), + Keycode2 => Key::Character("2".into()), + Keycode3 => Key::Character("3".into()), + Keycode4 => Key::Character("4".into()), + Keycode5 => Key::Character("5".into()), + Keycode6 => Key::Character("6".into()), + Keycode7 => Key::Character("7".into()), + Keycode8 => Key::Character("8".into()), + Keycode9 => Key::Character("9".into()), + Star => Key::Character("*".into()), + Pound => Key::Character("#".into()), + A => Key::Character("a".into()), + B => Key::Character("b".into()), + C => Key::Character("c".into()), + D => Key::Character("d".into()), + E => Key::Character("e".into()), + F => Key::Character("f".into()), + G => Key::Character("g".into()), + H => Key::Character("h".into()), + I => Key::Character("i".into()), + J => Key::Character("j".into()), + K => Key::Character("k".into()), + L => Key::Character("l".into()), + M => Key::Character("m".into()), + N => Key::Character("n".into()), + O => Key::Character("o".into()), + P => Key::Character("p".into()), + Q => Key::Character("q".into()), + R => Key::Character("r".into()), + S => Key::Character("s".into()), + T => Key::Character("t".into()), + U => Key::Character("u".into()), + V => Key::Character("v".into()), + W => Key::Character("w".into()), + X => Key::Character("x".into()), + Y => Key::Character("y".into()), + Z => Key::Character("z".into()), + Comma => Key::Character(",".into()), + Period => Key::Character(".".into()), + Grave => Key::Character("`".into()), + Minus => Key::Character("-".into()), + Equals => Key::Character("=".into()), + LeftBracket => Key::Character("[".into()), + RightBracket => Key::Character("]".into()), + Backslash => Key::Character("\\".into()), + Semicolon => Key::Character(";".into()), + Apostrophe => Key::Character("'".into()), + Slash => Key::Character("/".into()), + At => Key::Character("@".into()), + Plus => Key::Character("+".into()), + //------------------------------------------------------------------------------- + DpadUp => Key::Named(NamedKey::ArrowUp), + DpadDown => Key::Named(NamedKey::ArrowDown), + DpadLeft => Key::Named(NamedKey::ArrowLeft), + DpadRight => Key::Named(NamedKey::ArrowRight), + DpadCenter => Key::Named(NamedKey::Enter), + + VolumeUp => Key::Named(NamedKey::AudioVolumeUp), + VolumeDown => Key::Named(NamedKey::AudioVolumeDown), + Power => Key::Named(NamedKey::Power), + Camera => Key::Named(NamedKey::Camera), + Clear => Key::Named(NamedKey::Clear), + + AltLeft => Key::Named(NamedKey::Alt), + AltRight => Key::Named(NamedKey::Alt), + ShiftLeft => Key::Named(NamedKey::Shift), + ShiftRight => Key::Named(NamedKey::Shift), + Tab => Key::Named(NamedKey::Tab), + Space => Key::Character(" ".into()), + Sym => Key::Named(NamedKey::Symbol), + Explorer => Key::Named(NamedKey::LaunchWebBrowser), + Envelope => Key::Named(NamedKey::LaunchMail), + Enter => Key::Named(NamedKey::Enter), + Del => Key::Named(NamedKey::Backspace), + + // According to https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_NUM + Num => Key::Named(NamedKey::Alt), + + Headsethook => Key::Named(NamedKey::HeadsetHook), + Focus => Key::Named(NamedKey::CameraFocus), + + Notification => Key::Named(NamedKey::Notification), + Search => Key::Named(NamedKey::BrowserSearch), + MediaPlayPause => Key::Named(NamedKey::MediaPlayPause), + MediaStop => Key::Named(NamedKey::MediaStop), + MediaNext => Key::Named(NamedKey::MediaTrackNext), + MediaPrevious => Key::Named(NamedKey::MediaTrackPrevious), + MediaRewind => Key::Named(NamedKey::MediaRewind), + MediaFastForward => Key::Named(NamedKey::MediaFastForward), + Mute => Key::Named(NamedKey::MicrophoneVolumeMute), + PageUp => Key::Named(NamedKey::PageUp), + PageDown => Key::Named(NamedKey::PageDown), + + Escape => Key::Named(NamedKey::Escape), + ForwardDel => Key::Named(NamedKey::Delete), + CtrlLeft => Key::Named(NamedKey::Control), + CtrlRight => Key::Named(NamedKey::Control), + CapsLock => Key::Named(NamedKey::CapsLock), + ScrollLock => Key::Named(NamedKey::ScrollLock), + MetaLeft => Key::Named(NamedKey::Meta), + MetaRight => Key::Named(NamedKey::Meta), + Function => Key::Named(NamedKey::Fn), + Sysrq => Key::Named(NamedKey::PrintScreen), + Break => Key::Named(NamedKey::Pause), + MoveHome => Key::Named(NamedKey::Home), + MoveEnd => Key::Named(NamedKey::End), + Insert => Key::Named(NamedKey::Insert), + Forward => Key::Named(NamedKey::BrowserForward), + MediaPlay => Key::Named(NamedKey::MediaPlay), + MediaPause => Key::Named(NamedKey::MediaPause), + MediaClose => Key::Named(NamedKey::MediaClose), + MediaEject => Key::Named(NamedKey::Eject), + MediaRecord => Key::Named(NamedKey::MediaRecord), + F1 => Key::Named(NamedKey::F1), + F2 => Key::Named(NamedKey::F2), + F3 => Key::Named(NamedKey::F3), + F4 => Key::Named(NamedKey::F4), + F5 => Key::Named(NamedKey::F5), + F6 => Key::Named(NamedKey::F6), + F7 => Key::Named(NamedKey::F7), + F8 => Key::Named(NamedKey::F8), + F9 => Key::Named(NamedKey::F9), + F10 => Key::Named(NamedKey::F10), + F11 => Key::Named(NamedKey::F11), + F12 => Key::Named(NamedKey::F12), + NumLock => Key::Named(NamedKey::NumLock), + Numpad0 => Key::Character("0".into()), + Numpad1 => Key::Character("1".into()), + Numpad2 => Key::Character("2".into()), + Numpad3 => Key::Character("3".into()), + Numpad4 => Key::Character("4".into()), + Numpad5 => Key::Character("5".into()), + Numpad6 => Key::Character("6".into()), + Numpad7 => Key::Character("7".into()), + Numpad8 => Key::Character("8".into()), + Numpad9 => Key::Character("9".into()), + NumpadDivide => Key::Character("/".into()), + NumpadMultiply => Key::Character("*".into()), + NumpadSubtract => Key::Character("-".into()), + NumpadAdd => Key::Character("+".into()), + NumpadDot => Key::Character(".".into()), + NumpadComma => Key::Character(",".into()), + NumpadEnter => Key::Named(NamedKey::Enter), + NumpadEquals => Key::Character("=".into()), + NumpadLeftParen => Key::Character("(".into()), + NumpadRightParen => Key::Character(")".into()), + + VolumeMute => Key::Named(NamedKey::AudioVolumeMute), + Info => Key::Named(NamedKey::Info), + ChannelUp => Key::Named(NamedKey::ChannelUp), + ChannelDown => Key::Named(NamedKey::ChannelDown), + ZoomIn => Key::Named(NamedKey::ZoomIn), + ZoomOut => Key::Named(NamedKey::ZoomOut), + Tv => Key::Named(NamedKey::TV), + Guide => Key::Named(NamedKey::Guide), + Dvr => Key::Named(NamedKey::DVR), + Bookmark => Key::Named(NamedKey::BrowserFavorites), + Captions => Key::Named(NamedKey::ClosedCaptionToggle), + Settings => Key::Named(NamedKey::Settings), + TvPower => Key::Named(NamedKey::TVPower), + TvInput => Key::Named(NamedKey::TVInput), + StbPower => Key::Named(NamedKey::STBPower), + StbInput => Key::Named(NamedKey::STBInput), + AvrPower => Key::Named(NamedKey::AVRPower), + AvrInput => Key::Named(NamedKey::AVRInput), + ProgRed => Key::Named(NamedKey::ColorF0Red), + ProgGreen => Key::Named(NamedKey::ColorF1Green), + ProgYellow => Key::Named(NamedKey::ColorF2Yellow), + ProgBlue => Key::Named(NamedKey::ColorF3Blue), + AppSwitch => Key::Named(NamedKey::AppSwitch), + LanguageSwitch => Key::Named(NamedKey::GroupNext), + MannerMode => Key::Named(NamedKey::MannerMode), + Keycode3dMode => Key::Named(NamedKey::TV3DMode), + Contacts => Key::Named(NamedKey::LaunchContacts), + Calendar => Key::Named(NamedKey::LaunchCalendar), + Music => Key::Named(NamedKey::LaunchMusicPlayer), + Calculator => Key::Named(NamedKey::LaunchApplication2), + ZenkakuHankaku => Key::Named(NamedKey::ZenkakuHankaku), + Eisu => Key::Named(NamedKey::Eisu), + Muhenkan => Key::Named(NamedKey::NonConvert), + Henkan => Key::Named(NamedKey::Convert), + KatakanaHiragana => Key::Named(NamedKey::HiraganaKatakana), + Kana => Key::Named(NamedKey::KanjiMode), + BrightnessDown => Key::Named(NamedKey::BrightnessDown), + BrightnessUp => Key::Named(NamedKey::BrightnessUp), + MediaAudioTrack => Key::Named(NamedKey::MediaAudioTrack), + Sleep => Key::Named(NamedKey::Standby), + Wakeup => Key::Named(NamedKey::WakeUp), + Pairing => Key::Named(NamedKey::Pairing), + MediaTopMenu => Key::Named(NamedKey::MediaTopMenu), + LastChannel => Key::Named(NamedKey::MediaLast), + TvDataService => Key::Named(NamedKey::TVDataService), + VoiceAssist => Key::Named(NamedKey::VoiceDial), + TvRadioService => Key::Named(NamedKey::TVRadioService), + TvTeletext => Key::Named(NamedKey::Teletext), + TvNumberEntry => Key::Named(NamedKey::TVNumberEntry), + TvTerrestrialAnalog => Key::Named(NamedKey::TVTerrestrialAnalog), + TvTerrestrialDigital => Key::Named(NamedKey::TVTerrestrialDigital), + TvSatellite => Key::Named(NamedKey::TVSatellite), + TvSatelliteBs => Key::Named(NamedKey::TVSatelliteBS), + TvSatelliteCs => Key::Named(NamedKey::TVSatelliteCS), + TvSatelliteService => Key::Named(NamedKey::TVSatelliteToggle), + TvNetwork => Key::Named(NamedKey::TVNetwork), + TvAntennaCable => Key::Named(NamedKey::TVAntennaCable), + TvInputHdmi1 => Key::Named(NamedKey::TVInputHDMI1), + TvInputHdmi2 => Key::Named(NamedKey::TVInputHDMI2), + TvInputHdmi3 => Key::Named(NamedKey::TVInputHDMI3), + TvInputHdmi4 => Key::Named(NamedKey::TVInputHDMI4), + TvInputComposite1 => Key::Named(NamedKey::TVInputComposite1), + TvInputComposite2 => Key::Named(NamedKey::TVInputComposite2), + TvInputComponent1 => Key::Named(NamedKey::TVInputComponent1), + TvInputComponent2 => Key::Named(NamedKey::TVInputComponent2), + TvInputVga1 => Key::Named(NamedKey::TVInputVGA1), + TvAudioDescription => Key::Named(NamedKey::TVAudioDescription), + TvAudioDescriptionMixUp => Key::Named(NamedKey::TVAudioDescriptionMixUp), + TvAudioDescriptionMixDown => Key::Named(NamedKey::TVAudioDescriptionMixDown), + TvZoomMode => Key::Named(NamedKey::ZoomToggle), + TvContentsMenu => Key::Named(NamedKey::TVContentsMenu), + TvMediaContextMenu => Key::Named(NamedKey::TVMediaContext), + TvTimerProgramming => Key::Named(NamedKey::TVTimer), + Help => Key::Named(NamedKey::Help), + NavigatePrevious => Key::Named(NamedKey::NavigatePrevious), + NavigateNext => Key::Named(NamedKey::NavigateNext), + NavigateIn => Key::Named(NamedKey::NavigateIn), + NavigateOut => Key::Named(NamedKey::NavigateOut), + MediaSkipForward => Key::Named(NamedKey::MediaSkipForward), + MediaSkipBackward => Key::Named(NamedKey::MediaSkipBackward), + MediaStepForward => Key::Named(NamedKey::MediaStepForward), + MediaStepBackward => Key::Named(NamedKey::MediaStepBackward), + Cut => Key::Named(NamedKey::Cut), + Copy => Key::Named(NamedKey::Copy), + Paste => Key::Named(NamedKey::Paste), + Refresh => Key::Named(NamedKey::BrowserRefresh), + + // ----------------------------------------------------------------- + // Keycodes that don't have a logical Key mapping + // ----------------------------------------------------------------- + Unknown => Key::Unidentified(native), + + // Can be added on demand + SoftLeft => Key::Unidentified(native), + SoftRight => Key::Unidentified(native), + + Menu => Key::Unidentified(native), + + Pictsymbols => Key::Unidentified(native), + SwitchCharset => Key::Unidentified(native), + + // ----------------------------------------------------------------- + // Gamepad events should be exposed through a separate API, not + // keyboard events + ButtonA => Key::Unidentified(native), + ButtonB => Key::Unidentified(native), + ButtonC => Key::Unidentified(native), + ButtonX => Key::Unidentified(native), + ButtonY => Key::Unidentified(native), + ButtonZ => Key::Unidentified(native), + ButtonL1 => Key::Unidentified(native), + ButtonR1 => Key::Unidentified(native), + ButtonL2 => Key::Unidentified(native), + ButtonR2 => Key::Unidentified(native), + ButtonThumbl => Key::Unidentified(native), + ButtonThumbr => Key::Unidentified(native), + ButtonStart => Key::Unidentified(native), + ButtonSelect => Key::Unidentified(native), + ButtonMode => Key::Unidentified(native), + // ----------------------------------------------------------------- + Window => Key::Unidentified(native), + + Button1 => Key::Unidentified(native), + Button2 => Key::Unidentified(native), + Button3 => Key::Unidentified(native), + Button4 => Key::Unidentified(native), + Button5 => Key::Unidentified(native), + Button6 => Key::Unidentified(native), + Button7 => Key::Unidentified(native), + Button8 => Key::Unidentified(native), + Button9 => Key::Unidentified(native), + Button10 => Key::Unidentified(native), + Button11 => Key::Unidentified(native), + Button12 => Key::Unidentified(native), + Button13 => Key::Unidentified(native), + Button14 => Key::Unidentified(native), + Button15 => Key::Unidentified(native), + Button16 => Key::Unidentified(native), + + Yen => Key::Unidentified(native), + Ro => Key::Unidentified(native), + + Assist => Key::Unidentified(native), + + Keycode11 => Key::Unidentified(native), + Keycode12 => Key::Unidentified(native), + + StemPrimary => Key::Unidentified(native), + Stem1 => Key::Unidentified(native), + Stem2 => Key::Unidentified(native), + Stem3 => Key::Unidentified(native), + + DpadUpLeft => Key::Unidentified(native), + DpadDownLeft => Key::Unidentified(native), + DpadUpRight => Key::Unidentified(native), + DpadDownRight => Key::Unidentified(native), + + SoftSleep => Key::Unidentified(native), + + SystemNavigationUp => Key::Unidentified(native), + SystemNavigationDown => Key::Unidentified(native), + SystemNavigationLeft => Key::Unidentified(native), + SystemNavigationRight => Key::Unidentified(native), + + AllApps => Key::Unidentified(native), + ThumbsUp => Key::Unidentified(native), + ThumbsDown => Key::Unidentified(native), + ProfileSwitch => Key::Unidentified(native), + + // It's always possible that new versions of Android could introduce + // key codes we can't know about at compile time. + _ => Key::Unidentified(native), + }, + } +} + +pub fn to_location(keycode: Keycode) -> KeyLocation { + use android_activity::input::Keycode::*; + + match keycode { + AltLeft => KeyLocation::Left, + AltRight => KeyLocation::Right, + ShiftLeft => KeyLocation::Left, + ShiftRight => KeyLocation::Right, + + // According to https://developer.android.com/reference/android/view/KeyEvent#KEYCODE_NUM + Num => KeyLocation::Left, + + CtrlLeft => KeyLocation::Left, + CtrlRight => KeyLocation::Right, + MetaLeft => KeyLocation::Left, + MetaRight => KeyLocation::Right, + + NumLock => KeyLocation::Numpad, + Numpad0 => KeyLocation::Numpad, + Numpad1 => KeyLocation::Numpad, + Numpad2 => KeyLocation::Numpad, + Numpad3 => KeyLocation::Numpad, + Numpad4 => KeyLocation::Numpad, + Numpad5 => KeyLocation::Numpad, + Numpad6 => KeyLocation::Numpad, + Numpad7 => KeyLocation::Numpad, + Numpad8 => KeyLocation::Numpad, + Numpad9 => KeyLocation::Numpad, + NumpadDivide => KeyLocation::Numpad, + NumpadMultiply => KeyLocation::Numpad, + NumpadSubtract => KeyLocation::Numpad, + NumpadAdd => KeyLocation::Numpad, + NumpadDot => KeyLocation::Numpad, + NumpadComma => KeyLocation::Numpad, + NumpadEnter => KeyLocation::Numpad, + NumpadEquals => KeyLocation::Numpad, + NumpadLeftParen => KeyLocation::Numpad, + NumpadRightParen => KeyLocation::Numpad, + + _ => KeyLocation::Standard, + } +} diff --git a/patches/winit-android/src/lib.rs b/patches/winit-android/src/lib.rs new file mode 100644 index 0000000000..1230cf9752 --- /dev/null +++ b/patches/winit-android/src/lib.rs @@ -0,0 +1,166 @@ +//! # Android +//! +//! The Android backend builds on (and exposes types from) the [`ndk`](https://docs.rs/ndk/) crate. +//! +//! Native Android applications need some form of "glue" crate that is responsible +//! for defining the main entry point for your Rust application as well as tracking +//! various life-cycle events and synchronizing with the main JVM thread. +//! +//! Winit uses the [android-activity](https://docs.rs/android-activity/) as a +//! glue crate (prior to `0.28` it used +//! [ndk-glue](https://github.com/rust-windowing/android-ndk-rs/tree/master/ndk-glue)). +//! +//! The version of the glue crate that your application depends on _must_ match the +//! version that Winit depends on because the glue crate is responsible for your +//! application's main entry point. If Cargo resolves multiple versions, they will +//! clash. +//! +//! `winit` glue compatibility table: +//! +//! | winit | ndk-glue | +//! | :---: | :--------------------------: | +//! | 0.30 | `android-activity = "0.6"` | +//! | 0.29 | `android-activity = "0.5"` | +//! | 0.28 | `android-activity = "0.4"` | +//! | 0.27 | `ndk-glue = "0.7"` | +//! | 0.26 | `ndk-glue = "0.5"` | +//! | 0.25 | `ndk-glue = "0.3"` | +//! | 0.24 | `ndk-glue = "0.2"` | +//! +//! The recommended way to avoid a conflict with the glue version is to avoid explicitly +//! depending on the `android-activity` crate, and instead consume the API that +//! is re-exported by Winit under `winit::platform::android::activity::*` +//! +//! Running on an Android device needs a dynamic system library. Add this to Cargo.toml: +//! +//! ```toml +//! [lib] +//! name = "main" +//! crate-type = ["cdylib"] +//! ``` +//! +//! All Android applications are based on an `Activity` subclass, and the +//! `android-activity` crate is designed to support different choices for this base +//! class. Your application _must_ specify the base class it needs via a feature flag: +//! +//! | Base Class | Feature Flag | Notes | +//! | :--------------: | :---------------: | :-----: | +//! | `NativeActivity` | `android-native-activity` | Built-in to Android - it is possible to use without compiling any Java or Kotlin code. Java or Kotlin code may be needed to subclass `NativeActivity` to access some platform features. It does not derive from the [`AndroidAppCompat`] base class.| +//! | [`GameActivity`] | `android-game-activity` | Derives from [`AndroidAppCompat`], a defacto standard `Activity` base class that helps support a wider range of Android versions. Requires a build system that can compile Java or Kotlin and fetch Android dependencies from a [Maven repository][agdk_jetpack] (or link with an embedded [release][agdk_releases] of [`GameActivity`]) | +//! +//! [`GameActivity`]: https://developer.android.com/games/agdk/game-activity +//! [`GameTextInput`]: https://developer.android.com/games/agdk/add-support-for-text-input +//! [`AndroidAppCompat`]: https://developer.android.com/reference/androidx/appcompat/app/AppCompatActivity +//! [agdk_jetpack]: https://developer.android.com/jetpack/androidx/releases/games +//! [agdk_releases]: https://developer.android.com/games/agdk/download#agdk-libraries +//! [Gradle]: https://developer.android.com/studio/build +//! +//! For more details, refer to these `android-activity` [example applications](https://github.com/rust-mobile/android-activity/tree/main/examples). +//! +//! ## Converting from `ndk-glue` to `android-activity` +//! +//! If your application is currently based on `NativeActivity` via the `ndk-glue` crate and building +//! with `cargo apk`, then the minimal changes would be: +//! 1. Remove `ndk-glue` from your `Cargo.toml` +//! 2. Enable the `"android-native-activity"` feature for Winit: `winit = { version = +//! "0.31.0-beta.3", features = [ "android-native-activity" ] }` +//! 3. Add an `android_main` entrypoint (as above), instead of using the '`[ndk_glue::main]` proc +//! macro from `ndk-macros` (optionally add a dependency on `android_logger` and initialize +//! logging as above). +//! 4. Pass a clone of the `AndroidApp` that your application receives to Winit when building your +//! event loop (as shown above). +#![cfg(target_os = "android")] +#![warn(clippy::exhaustive_enums)] + +mod event_loop; +mod keycodes; + +use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop; +use winit_core::window::Window as CoreWindow; + +use self::activity::{AndroidApp, ConfigurationRef, Rect}; +pub use crate::event_loop::{ + ActiveEventLoop, EventLoop, EventLoopProxy, PlatformSpecificEventLoopAttributes, + PlatformSpecificWindowAttributes, Window, +}; + +/// Additional methods on [`EventLoop`] that are specific to Android. +pub trait EventLoopExtAndroid { + /// Get the [`AndroidApp`] which was used to create this event loop. + fn android_app(&self) -> &AndroidApp; +} + +/// Additional methods on [`ActiveEventLoop`] that are specific to Android. +pub trait ActiveEventLoopExtAndroid { + /// Get the [`AndroidApp`] which was used to create this event loop. + fn android_app(&self) -> &AndroidApp; +} + +/// Additional methods on [`Window`] that are specific to Android. +pub trait WindowExtAndroid { + fn content_rect(&self) -> Rect; + + fn config(&self) -> ConfigurationRef; +} + +impl WindowExtAndroid for dyn CoreWindow + '_ { + fn content_rect(&self) -> Rect { + let window = self.cast_ref::().unwrap(); + window.content_rect() + } + + fn config(&self) -> ConfigurationRef { + let window = self.cast_ref::().unwrap(); + window.config() + } +} + +impl ActiveEventLoopExtAndroid for dyn CoreActiveEventLoop + '_ { + fn android_app(&self) -> &AndroidApp { + let event_loop = self.cast_ref::().unwrap(); + &event_loop.app + } +} + +pub trait EventLoopBuilderExtAndroid { + /// Associates the [`AndroidApp`] that was passed to `android_main()` with the event loop + /// + /// This must be called on Android since the [`AndroidApp`] is not global state. + fn with_android_app(&mut self, app: AndroidApp) -> &mut Self; + + /// Calling this will mark the volume keys to be manually handled by the application + /// + /// Default is to let the operating system handle the volume keys + fn handle_volume_keys(&mut self) -> &mut Self; +} + +/// Re-export of the `android_activity` API +/// +/// Winit re-exports the `android_activity` API for convenience so that most +/// applications can rely on the Winit crate to resolve the required version of +/// `android_activity` and avoid any chance of a conflict between Winit and the +/// application crate. +/// +/// Unlike most libraries there can only be a single implementation +/// of the `android_activity` glue crate linked with an application because +/// it is responsible for the application's `android_main()` entry point. +/// +/// Since Winit depends on a specific version of `android_activity` the simplest +/// way to avoid creating a conflict is for applications to avoid explicitly +/// depending on the `android_activity` crate, and instead consume the API that +/// is re-exported by Winit. +/// +/// For compatibility applications should then import the [`AndroidApp`] type for +/// their `android_main(app: AndroidApp)` function like: +/// ```rust +/// #[cfg(target_os = "android")] +/// use winit::platform::android::activity::AndroidApp; +/// ``` +pub mod activity { + // We enable the `"native-activity"` feature just so that we can build the + // docs, but it'll be very confusing for users to see the docs with that + // feature enabled, so we avoid inlining it so that they're forced to view + // it on the crate's own docs.rs page. + #[doc(no_inline)] + pub use android_activity::*; +} From d04cf78fc9028ab62d2adc54b969dbe087052c83 Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Fri, 23 Oct 2026 15:18:45 -0600 Subject: [PATCH 2/3] fix(dom): delete the text either side of the selection for Ime::DeleteSurrounding A text input ignored `Ime::DeleteSurrounding` (a `TODO`), so an input method that deletes before its next step (a correction, a word composed again, Backspace from a keyboard that edits text rather than sending keys) left the old text in place and typed after it. It now deletes that many bytes after and before the selection, with parley's `delete_bytes_after_selection` and `delete_bytes_before_selection`, and reports an input event. Behavioural-change: a text input deletes the text an `Ime::DeleteSurrounding` names, and fires `input`, where it did nothing. --- packages/blitz-dom/src/node/text.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/blitz-dom/src/node/text.rs b/packages/blitz-dom/src/node/text.rs index e2318c5a7b..dfa02a1dbf 100644 --- a/packages/blitz-dom/src/node/text.rs +++ b/packages/blitz-dom/src/node/text.rs @@ -794,10 +794,18 @@ impl TextInputData { before_bytes, after_bytes, } => { - let _ = before_bytes; - let _ = after_bytes; - // TODO - None + // The input method's text either side of the selection, gone + // before its next step: a correction, a word composed again, + // or Backspace from a keyboard that edits text rather than + // sending keys (Android's, through an application's own + // connection to it). + if let Some(after) = std::num::NonZeroUsize::new(after_bytes) { + driver.delete_bytes_after_selection(after); + } + if let Some(before) = std::num::NonZeroUsize::new(before_bytes) { + driver.delete_bytes_before_selection(before); + } + (before_bytes > 0 || after_bytes > 0).then_some(GeneratedTextInputEvent::Input) } } } From 99eee375a0fa12e617f2d312fe32f0b2d775257d Mon Sep 17 00:00:00 2001 From: Adam Harris Date: Fri, 23 Oct 2026 15:18:45 -0600 Subject: [PATCH 3/3] feat(winit-android): take Ime events from the application, and end the event loop on Destroy A NativeActivity gives an input method no InputConnection, so winit-android reported no `WindowEvent::Ime`, and a soft keyboard could only send keys. `winit_android::ime::send` takes an `Ime` event from any thread (from an application's own input connection, a Java view over the activity) and the event loop delivers it to the window as `WindowEvent::Ime`. `MainEvent::Destroy` was a `TODO`, and the loop outlived its activity in a process Android keeps for the next one; launched again, the app called `android_main` there, winit refused a second event loop, and nothing drew. The loop now exits on `Destroy`, so `run_app` returns. Vendored unmodified in the commit before; patches/winit-android/PATCH.md says what changed and why. FORK.md lists it. Behavioural-change: on Android, `run_app` returns when the activity is destroyed, instead of the event loop running on. --- FORK.md | 9 ++++- patches/winit-android/PATCH.md | 45 +++++++++++++++++++++++++ patches/winit-android/src/event_loop.rs | 27 ++++++++++++--- patches/winit-android/src/ime.rs | 39 +++++++++++++++++++++ patches/winit-android/src/lib.rs | 2 ++ 5 files changed, 116 insertions(+), 6 deletions(-) create mode 100644 patches/winit-android/PATCH.md create mode 100644 patches/winit-android/src/ime.rs diff --git a/FORK.md b/FORK.md index e0a8753d6a..d0468e3245 100644 --- a/FORK.md +++ b/FORK.md @@ -110,7 +110,14 @@ published crates (`apps/diaryx/patches` there): And the winit backends dioxus-native pins, patched, in `patches/`: `winit-appkit` (the Mac's text input client answers about the text round the caret: accents, the emoji picker, dictation) and `winit-uikit` (the UIScene -lifecycle iOS 27 requires). They came from leaf's `patches/`. +lifecycle iOS 27 requires), which came from leaf's `patches/`; and +`winit-android` (`Ime` events an application's own input connection hands +it, and an event loop that ends with its activity), from Diaryx's Android +app. + +And in Blitz's text input, `Ime::DeleteSurrounding`, which was a `TODO`: the +text either side of the selection deleted, as an input method asks before a +correction or a recomposed word. And `anyrender_vello`, from Diaryx's app: a default frame latency of 2, not 1, so that on Metal a frame does not wait for the one on screen before it can diff --git a/patches/winit-android/PATCH.md b/patches/winit-android/PATCH.md new file mode 100644 index 0000000000..a03ecaf3e0 --- /dev/null +++ b/patches/winit-android/PATCH.md @@ -0,0 +1,45 @@ +# winit-android 0.31.0-beta.3, patched + +The crate dioxus-native pins, patched for Diaryx's Android app and kept +here, in diaryx-org's Blitz fork, beside the Blitz it is built with. It is +vendored unmodified in the commit before the patch. Every change is marked +`leaf patch` in the source. + +Consumers take it with `[patch.crates-io]` in their root manifest, from this +repository's branch the rest of their Blitz comes from, `{ git = +"https://github.com/diaryx-org/blitz", branch = "leaf" }` — cargo finds the +crate by name in the repository. + +## What was wrong + +- **No input method events.** A `NativeActivity` gives an input method no + `InputConnection`, so winit-android reports no `WindowEvent::Ime` at all, + and a soft keyboard can only send keys: no accents, emoji, suggestions, + corrections, swipe or voice typing. An application can give the input + method a connection of its own (a Java `View` over the activity, with + `onCreateInputConnection`), but had no way to hand winit what it heard. +- **The event loop outlived its activity.** `MainEvent::Destroy` was a + `TODO`: the loop went on after the activity was gone, in a process Android + keeps for the next activity. When the app was launched again, Android + called `android_main` again in that process, where winit refuses a second + event loop (`RecreationAttempt`), and the app never drew past its splash + screen. + +## What the patch does + +- `winit_android::ime::send(Ime)` (`ime.rs`): queue an `Ime` event from any + thread and wake the event loop, which delivers it to the window as + `WindowEvent::Ime` in the next iteration, after the input queue's events. + A wake with an event queued is not ignored as a spurious one. +- On `MainEvent::Destroy`, the event loop exits, so `run_app` returns and the + application is dropped. An application ends its process there, or keeps + it and makes nothing more of winit. + +An application that never calls `ime::send` gets the old behaviour, except +that the event loop now ends with its activity. + +## Upstream + +Not prepared. Upstream winit has the same `TODO` for `Destroy`; the `Ime` +queue is a stopgap for winit giving the Android backend an input connection +of its own (GameActivity's `TextInputState` covers it for that activity). diff --git a/patches/winit-android/src/event_loop.rs b/patches/winit-android/src/event_loop.rs index ec8abe4d96..425ef4cb0b 100644 --- a/patches/winit-android/src/event_loop.rs +++ b/patches/winit-android/src/event_loop.rs @@ -141,6 +141,8 @@ impl EventLoop { ); let event_loop_proxy = Arc::new(EventLoopProxy::new(android_app.create_waker())); + // leaf patch: wake for an `Ime` event the application sends. + crate::ime::set_waker(android_app.create_waker()); let redraw_flag = SharedFlag::new(); @@ -246,9 +248,13 @@ impl EventLoop { app.suspended(self.window_target()); }, MainEvent::Destroy => { - // XXX: maybe exit mainloop to drop things before being - // killed by the OS? - warn!("TODO: forward onDestroy notification to application"); + // leaf patch: the activity is gone, so end the event loop + // and drop the application with it. Left running, the + // loop outlived its activity in a process Android keeps + // for the next one, whose `android_main` then could not + // make an event loop (`RecreationAttempt`) and never drew. + debug!("App Destroyed - exiting the event loop"); + self.window_target.exit(); }, MainEvent::InsetsChanged { .. } => { // XXX: how to forward this state to applications? @@ -281,6 +287,13 @@ impl EventLoop { }, } + // leaf patch: the input method's events, from the application's own + // connection to it (`ime.rs`). + for ime in crate::ime::take() { + let event = event::WindowEvent::Ime(ime); + app.window_event(&self.window_target, GLOBAL_WINDOW, event); + } + if self.window_target.event_loop_proxy.wake_up.swap(false, Ordering::Relaxed) { app.proxy_wake_up(&self.window_target); } @@ -572,7 +585,9 @@ impl EventLoop { timeout = if self.running && (self.pending_redraw - || self.window_target.event_loop_proxy.wake_up.load(Ordering::Relaxed)) + || self.window_target.event_loop_proxy.wake_up.load(Ordering::Relaxed) + // leaf patch + || crate::ime::pending()) { // If we already have work to do then we don't want to block on the next poll Some(Duration::ZERO) @@ -605,7 +620,9 @@ impl EventLoop { self.pending_redraw |= self.redraw_flag.get_and_reset(); if !self.running || (!self.pending_redraw - && !self.window_target.event_loop_proxy.wake_up.load(Ordering::Relaxed)) + && !self.window_target.event_loop_proxy.wake_up.load(Ordering::Relaxed) + // leaf patch: an `Ime` event to deliver. + && !crate::ime::pending()) { return; } diff --git a/patches/winit-android/src/ime.rs b/patches/winit-android/src/ime.rs new file mode 100644 index 0000000000..d6e9839e8e --- /dev/null +++ b/patches/winit-android/src/ime.rs @@ -0,0 +1,39 @@ +//! leaf patch: input method events from the application's own view. +//! +//! A `NativeActivity` gives an input method no `InputConnection`, so winit +//! has nothing to report `Ime` events from, and a soft keyboard can only send +//! keys. An application that gives the input method a connection of its own +//! — a Java `View` with `onCreateInputConnection`, say — hands what it hears +//! here with [`send`], from any thread, and the event loop delivers each one +//! to the window as `WindowEvent::Ime`, in order, as the other backends +//! report their input methods. + +use std::collections::VecDeque; +use std::sync::Mutex; + +use android_activity::AndroidAppWaker; +pub use winit_core::event::Ime; + +static QUEUE: Mutex> = Mutex::new(VecDeque::new()); +static WAKER: Mutex> = Mutex::new(None); + +/// Deliver `ime` to the window as `WindowEvent::Ime`, waking the event loop +/// for it. Events sent before the event loop is made wait for it. +pub fn send(ime: Ime) { + QUEUE.lock().unwrap().push_back(ime); + if let Some(waker) = WAKER.lock().unwrap().as_ref() { + waker.wake(); + } +} + +pub(crate) fn set_waker(waker: AndroidAppWaker) { + *WAKER.lock().unwrap() = Some(waker); +} + +pub(crate) fn pending() -> bool { + !QUEUE.lock().unwrap().is_empty() +} + +pub(crate) fn take() -> VecDeque { + std::mem::take(&mut *QUEUE.lock().unwrap()) +} diff --git a/patches/winit-android/src/lib.rs b/patches/winit-android/src/lib.rs index 1230cf9752..8dd1e0763c 100644 --- a/patches/winit-android/src/lib.rs +++ b/patches/winit-android/src/lib.rs @@ -73,6 +73,8 @@ #![warn(clippy::exhaustive_enums)] mod event_loop; +// leaf patch: `Ime` events from the application (`ime.rs`). +pub mod ime; mod keycodes; use winit_core::event_loop::ActiveEventLoop as CoreActiveEventLoop;