From e78ece55e50dbff63d1be80b5a626a767183ada4 Mon Sep 17 00:00:00 2001 From: Robert Bragg Date: Mon, 7 Aug 2023 22:58:15 +0100 Subject: [PATCH] Consolidate input types to avoid portability hazards The following types have been moved from game_activity/input.rs to input.rs so they can be shared by both backends: Axis, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction, MotionEventFlags This addresses a portability hazard whereby code (such as Winit) would inadvertently use the `ndk` type which works OK with the native-activity backend but then wouldn't compile against the game-activity backend. The alternative of consolidating on the `ndk::events` types instead was considered but we've repeatedly needed to diverge from the `ndk` API for the sake of maintaining a consistent input API across the `game-activity` and `native-activity` backends (input is an area where the backends differ significantly in their implementation) and so it generally seems slightly preferable to consolidate on types from this crate (though it shouldn't make much difference for these types which are almost direct bindings from ndk_sys). The types can be converted to their `ndk::events` counterpart via the `From` trait. For now some of the `From` trait implementations rely on `try_from().unwrap()` but the intention is to replace these with infallible implementations once we bump the MSRV > 1.66 where we can use `num_enum::FromPrimitive` after adding a catch-all `Other(u32)` to these enums. --- android-activity/src/game_activity/input.rs | 608 +--------------- android-activity/src/game_activity/mod.rs | 4 +- android-activity/src/input.rs | 673 ++++++++++++++++++ android-activity/src/input/sdk.rs | 2 +- android-activity/src/native_activity/input.rs | 26 +- android-activity/src/native_activity/mod.rs | 6 +- 6 files changed, 696 insertions(+), 623 deletions(-) diff --git a/android-activity/src/game_activity/input.rs b/android-activity/src/game_activity/input.rs index 29b33bf..b5d5407 100644 --- a/android-activity/src/game_activity/input.rs +++ b/android-activity/src/game_activity/input.rs @@ -13,11 +13,13 @@ // The `Class` was also bound differently to `android-ndk-rs` considering how the class is defined // by masking bits from the `Source`. -use num_enum::{IntoPrimitive, TryFromPrimitive}; use std::convert::TryInto; use crate::activity_impl::ffi::{GameActivityKeyEvent, GameActivityMotionEvent}; -use crate::input::{Class, Source}; +use crate::input::{ + Axis, ButtonState, Class, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, + MotionAction, MotionEventFlags, Source, ToolType, +}; // Note: try to keep this wrapper API compatible with the AInputEvent API if possible @@ -29,83 +31,6 @@ pub enum InputEvent<'a> { TextEvent(crate::input::TextInputState), } -/// A bitfield representing the state of modifier keys during an event. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-25) -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct MetaState(pub u32); - -impl MetaState { - #[inline] - pub fn alt_on(self) -> bool { - self.0 & ndk_sys::AMETA_ALT_ON != 0 - } - #[inline] - pub fn alt_left_on(self) -> bool { - self.0 & ndk_sys::AMETA_ALT_LEFT_ON != 0 - } - #[inline] - pub fn alt_right_on(self) -> bool { - self.0 & ndk_sys::AMETA_ALT_RIGHT_ON != 0 - } - #[inline] - pub fn shift_on(self) -> bool { - self.0 & ndk_sys::AMETA_SHIFT_ON != 0 - } - #[inline] - pub fn shift_left_on(self) -> bool { - self.0 & ndk_sys::AMETA_SHIFT_LEFT_ON != 0 - } - #[inline] - pub fn shift_right_on(self) -> bool { - self.0 & ndk_sys::AMETA_SHIFT_RIGHT_ON != 0 - } - #[inline] - pub fn sym_on(self) -> bool { - self.0 & ndk_sys::AMETA_SYM_ON != 0 - } - #[inline] - pub fn function_on(self) -> bool { - self.0 & ndk_sys::AMETA_FUNCTION_ON != 0 - } - #[inline] - pub fn ctrl_on(self) -> bool { - self.0 & ndk_sys::AMETA_CTRL_ON != 0 - } - #[inline] - pub fn ctrl_left_on(self) -> bool { - self.0 & ndk_sys::AMETA_CTRL_LEFT_ON != 0 - } - #[inline] - pub fn ctrl_right_on(self) -> bool { - self.0 & ndk_sys::AMETA_CTRL_RIGHT_ON != 0 - } - #[inline] - pub fn meta_on(self) -> bool { - self.0 & ndk_sys::AMETA_META_ON != 0 - } - #[inline] - pub fn meta_left_on(self) -> bool { - self.0 & ndk_sys::AMETA_META_LEFT_ON != 0 - } - #[inline] - pub fn meta_right_on(self) -> bool { - self.0 & ndk_sys::AMETA_META_RIGHT_ON != 0 - } - #[inline] - pub fn caps_lock_on(self) -> bool { - self.0 & ndk_sys::AMETA_CAPS_LOCK_ON != 0 - } - #[inline] - pub fn num_lock_on(self) -> bool { - self.0 & ndk_sys::AMETA_NUM_LOCK_ON != 0 - } - #[inline] - pub fn scroll_lock_on(self) -> bool { - self.0 & ndk_sys::AMETA_SCROLL_LOCK_ON != 0 - } -} - /// A motion event. /// /// For general discussion of motion events in Android, see [the relevant @@ -115,170 +40,6 @@ pub struct MotionEvent<'a> { ga_event: &'a GameActivityMotionEvent, } -/// A motion action. -/// -/// See [the NDK -/// docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-29) -#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] -#[repr(u32)] -pub enum MotionAction { - Down = ndk_sys::AMOTION_EVENT_ACTION_DOWN, - Up = ndk_sys::AMOTION_EVENT_ACTION_UP, - Move = ndk_sys::AMOTION_EVENT_ACTION_MOVE, - Cancel = ndk_sys::AMOTION_EVENT_ACTION_CANCEL, - Outside = ndk_sys::AMOTION_EVENT_ACTION_OUTSIDE, - PointerDown = ndk_sys::AMOTION_EVENT_ACTION_POINTER_DOWN, - PointerUp = ndk_sys::AMOTION_EVENT_ACTION_POINTER_UP, - HoverMove = ndk_sys::AMOTION_EVENT_ACTION_HOVER_MOVE, - Scroll = ndk_sys::AMOTION_EVENT_ACTION_SCROLL, - HoverEnter = ndk_sys::AMOTION_EVENT_ACTION_HOVER_ENTER, - HoverExit = ndk_sys::AMOTION_EVENT_ACTION_HOVER_EXIT, - ButtonPress = ndk_sys::AMOTION_EVENT_ACTION_BUTTON_PRESS, - ButtonRelease = ndk_sys::AMOTION_EVENT_ACTION_BUTTON_RELEASE, -} - -/// An axis of a motion event. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-32) -#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] -#[repr(u32)] -pub enum Axis { - X = ndk_sys::AMOTION_EVENT_AXIS_X, - Y = ndk_sys::AMOTION_EVENT_AXIS_Y, - Pressure = ndk_sys::AMOTION_EVENT_AXIS_PRESSURE, - Size = ndk_sys::AMOTION_EVENT_AXIS_SIZE, - TouchMajor = ndk_sys::AMOTION_EVENT_AXIS_TOUCH_MAJOR, - TouchMinor = ndk_sys::AMOTION_EVENT_AXIS_TOUCH_MINOR, - ToolMajor = ndk_sys::AMOTION_EVENT_AXIS_TOOL_MAJOR, - ToolMinor = ndk_sys::AMOTION_EVENT_AXIS_TOOL_MINOR, - Orientation = ndk_sys::AMOTION_EVENT_AXIS_ORIENTATION, - Vscroll = ndk_sys::AMOTION_EVENT_AXIS_VSCROLL, - Hscroll = ndk_sys::AMOTION_EVENT_AXIS_HSCROLL, - Z = ndk_sys::AMOTION_EVENT_AXIS_Z, - Rx = ndk_sys::AMOTION_EVENT_AXIS_RX, - Ry = ndk_sys::AMOTION_EVENT_AXIS_RY, - Rz = ndk_sys::AMOTION_EVENT_AXIS_RZ, - HatX = ndk_sys::AMOTION_EVENT_AXIS_HAT_X, - HatY = ndk_sys::AMOTION_EVENT_AXIS_HAT_Y, - Ltrigger = ndk_sys::AMOTION_EVENT_AXIS_LTRIGGER, - Rtrigger = ndk_sys::AMOTION_EVENT_AXIS_RTRIGGER, - Throttle = ndk_sys::AMOTION_EVENT_AXIS_THROTTLE, - Rudder = ndk_sys::AMOTION_EVENT_AXIS_RUDDER, - Wheel = ndk_sys::AMOTION_EVENT_AXIS_WHEEL, - Gas = ndk_sys::AMOTION_EVENT_AXIS_GAS, - Brake = ndk_sys::AMOTION_EVENT_AXIS_BRAKE, - Distance = ndk_sys::AMOTION_EVENT_AXIS_DISTANCE, - Tilt = ndk_sys::AMOTION_EVENT_AXIS_TILT, - Scroll = ndk_sys::AMOTION_EVENT_AXIS_SCROLL, - RelativeX = ndk_sys::AMOTION_EVENT_AXIS_RELATIVE_X, - RelativeY = ndk_sys::AMOTION_EVENT_AXIS_RELATIVE_Y, - Generic1 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_1, - Generic2 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_2, - Generic3 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_3, - Generic4 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_4, - Generic5 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_5, - Generic6 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_6, - Generic7 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_7, - Generic8 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_8, - Generic9 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_9, - Generic10 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_10, - Generic11 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_11, - Generic12 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_12, - Generic13 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_13, - Generic14 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_14, - Generic15 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_15, - Generic16 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_16, -} - -/// The tool type of a pointer. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-48) -#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] -#[repr(u32)] -pub enum ToolType { - Unknown = ndk_sys::AMOTION_EVENT_TOOL_TYPE_UNKNOWN, - Finger = ndk_sys::AMOTION_EVENT_TOOL_TYPE_FINGER, - Stylus = ndk_sys::AMOTION_EVENT_TOOL_TYPE_STYLUS, - Mouse = ndk_sys::AMOTION_EVENT_TOOL_TYPE_MOUSE, - Eraser = ndk_sys::AMOTION_EVENT_TOOL_TYPE_ERASER, - Palm = ndk_sys::AMOTION_EVENT_TOOL_TYPE_PALM, -} - -/// A bitfield representing the state of buttons during a motion event. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-33) -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct ButtonState(pub u32); - -impl ButtonState { - #[inline] - pub fn primary(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_BUTTON_PRIMARY != 0 - } - #[inline] - pub fn secondary(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_BUTTON_SECONDARY != 0 - } - #[inline] - pub fn teriary(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_BUTTON_TERTIARY != 0 - } - #[inline] - pub fn back(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_BUTTON_BACK != 0 - } - #[inline] - pub fn forward(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_BUTTON_FORWARD != 0 - } - #[inline] - pub fn stylus_primary(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_PRIMARY != 0 - } - #[inline] - pub fn stylus_secondary(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_SECONDARY != 0 - } -} - -/// A bitfield representing which edges were touched by a motion event. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-31) -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct EdgeFlags(pub u32); - -impl EdgeFlags { - #[inline] - pub fn top(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_TOP != 0 - } - #[inline] - pub fn bottom(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_BOTTOM != 0 - } - #[inline] - pub fn left(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_LEFT != 0 - } - #[inline] - pub fn right(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_RIGHT != 0 - } -} - -/// Flags associated with this [`MotionEvent`]. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-30) -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct MotionEventFlags(pub u32); - -impl MotionEventFlags { - #[inline] - pub fn window_is_obscured(self) -> bool { - self.0 & ndk_sys::AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED != 0 - } -} - impl<'a> MotionEvent<'a> { pub(crate) fn new(ga_event: &'a GameActivityMotionEvent) -> Self { Self { ga_event } @@ -930,314 +691,6 @@ pub struct KeyEvent<'a> { ga_event: &'a GameActivityKeyEvent, } -/// Key actions. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-27) -#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] -#[repr(u32)] -pub enum KeyAction { - Down = ndk_sys::AKEY_EVENT_ACTION_DOWN, - Up = ndk_sys::AKEY_EVENT_ACTION_UP, - Multiple = ndk_sys::AKEY_EVENT_ACTION_MULTIPLE, -} - -/// Key codes. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-39) -#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] -#[repr(u32)] -pub enum Keycode { - Unknown = ndk_sys::AKEYCODE_UNKNOWN, - SoftLeft = ndk_sys::AKEYCODE_SOFT_LEFT, - SoftRight = ndk_sys::AKEYCODE_SOFT_RIGHT, - Home = ndk_sys::AKEYCODE_HOME, - Back = ndk_sys::AKEYCODE_BACK, - Call = ndk_sys::AKEYCODE_CALL, - Endcall = ndk_sys::AKEYCODE_ENDCALL, - Keycode0 = ndk_sys::AKEYCODE_0, - Keycode1 = ndk_sys::AKEYCODE_1, - Keycode2 = ndk_sys::AKEYCODE_2, - Keycode3 = ndk_sys::AKEYCODE_3, - Keycode4 = ndk_sys::AKEYCODE_4, - Keycode5 = ndk_sys::AKEYCODE_5, - Keycode6 = ndk_sys::AKEYCODE_6, - Keycode7 = ndk_sys::AKEYCODE_7, - Keycode8 = ndk_sys::AKEYCODE_8, - Keycode9 = ndk_sys::AKEYCODE_9, - Star = ndk_sys::AKEYCODE_STAR, - Pound = ndk_sys::AKEYCODE_POUND, - DpadUp = ndk_sys::AKEYCODE_DPAD_UP, - DpadDown = ndk_sys::AKEYCODE_DPAD_DOWN, - DpadLeft = ndk_sys::AKEYCODE_DPAD_LEFT, - DpadRight = ndk_sys::AKEYCODE_DPAD_RIGHT, - DpadCenter = ndk_sys::AKEYCODE_DPAD_CENTER, - VolumeUp = ndk_sys::AKEYCODE_VOLUME_UP, - VolumeDown = ndk_sys::AKEYCODE_VOLUME_DOWN, - Power = ndk_sys::AKEYCODE_POWER, - Camera = ndk_sys::AKEYCODE_CAMERA, - Clear = ndk_sys::AKEYCODE_CLEAR, - A = ndk_sys::AKEYCODE_A, - B = ndk_sys::AKEYCODE_B, - C = ndk_sys::AKEYCODE_C, - D = ndk_sys::AKEYCODE_D, - E = ndk_sys::AKEYCODE_E, - F = ndk_sys::AKEYCODE_F, - G = ndk_sys::AKEYCODE_G, - H = ndk_sys::AKEYCODE_H, - I = ndk_sys::AKEYCODE_I, - J = ndk_sys::AKEYCODE_J, - K = ndk_sys::AKEYCODE_K, - L = ndk_sys::AKEYCODE_L, - M = ndk_sys::AKEYCODE_M, - N = ndk_sys::AKEYCODE_N, - O = ndk_sys::AKEYCODE_O, - P = ndk_sys::AKEYCODE_P, - Q = ndk_sys::AKEYCODE_Q, - R = ndk_sys::AKEYCODE_R, - S = ndk_sys::AKEYCODE_S, - T = ndk_sys::AKEYCODE_T, - U = ndk_sys::AKEYCODE_U, - V = ndk_sys::AKEYCODE_V, - W = ndk_sys::AKEYCODE_W, - X = ndk_sys::AKEYCODE_X, - Y = ndk_sys::AKEYCODE_Y, - Z = ndk_sys::AKEYCODE_Z, - Comma = ndk_sys::AKEYCODE_COMMA, - Period = ndk_sys::AKEYCODE_PERIOD, - AltLeft = ndk_sys::AKEYCODE_ALT_LEFT, - AltRight = ndk_sys::AKEYCODE_ALT_RIGHT, - ShiftLeft = ndk_sys::AKEYCODE_SHIFT_LEFT, - ShiftRight = ndk_sys::AKEYCODE_SHIFT_RIGHT, - Tab = ndk_sys::AKEYCODE_TAB, - Space = ndk_sys::AKEYCODE_SPACE, - Sym = ndk_sys::AKEYCODE_SYM, - Explorer = ndk_sys::AKEYCODE_EXPLORER, - Envelope = ndk_sys::AKEYCODE_ENVELOPE, - Enter = ndk_sys::AKEYCODE_ENTER, - Del = ndk_sys::AKEYCODE_DEL, - Grave = ndk_sys::AKEYCODE_GRAVE, - Minus = ndk_sys::AKEYCODE_MINUS, - Equals = ndk_sys::AKEYCODE_EQUALS, - LeftBracket = ndk_sys::AKEYCODE_LEFT_BRACKET, - RightBracket = ndk_sys::AKEYCODE_RIGHT_BRACKET, - Backslash = ndk_sys::AKEYCODE_BACKSLASH, - Semicolon = ndk_sys::AKEYCODE_SEMICOLON, - Apostrophe = ndk_sys::AKEYCODE_APOSTROPHE, - Slash = ndk_sys::AKEYCODE_SLASH, - At = ndk_sys::AKEYCODE_AT, - Num = ndk_sys::AKEYCODE_NUM, - Headsethook = ndk_sys::AKEYCODE_HEADSETHOOK, - Focus = ndk_sys::AKEYCODE_FOCUS, - Plus = ndk_sys::AKEYCODE_PLUS, - Menu = ndk_sys::AKEYCODE_MENU, - Notification = ndk_sys::AKEYCODE_NOTIFICATION, - Search = ndk_sys::AKEYCODE_SEARCH, - MediaPlayPause = ndk_sys::AKEYCODE_MEDIA_PLAY_PAUSE, - MediaStop = ndk_sys::AKEYCODE_MEDIA_STOP, - MediaNext = ndk_sys::AKEYCODE_MEDIA_NEXT, - MediaPrevious = ndk_sys::AKEYCODE_MEDIA_PREVIOUS, - MediaRewind = ndk_sys::AKEYCODE_MEDIA_REWIND, - MediaFastForward = ndk_sys::AKEYCODE_MEDIA_FAST_FORWARD, - Mute = ndk_sys::AKEYCODE_MUTE, - PageUp = ndk_sys::AKEYCODE_PAGE_UP, - PageDown = ndk_sys::AKEYCODE_PAGE_DOWN, - Pictsymbols = ndk_sys::AKEYCODE_PICTSYMBOLS, - SwitchCharset = ndk_sys::AKEYCODE_SWITCH_CHARSET, - ButtonA = ndk_sys::AKEYCODE_BUTTON_A, - ButtonB = ndk_sys::AKEYCODE_BUTTON_B, - ButtonC = ndk_sys::AKEYCODE_BUTTON_C, - ButtonX = ndk_sys::AKEYCODE_BUTTON_X, - ButtonY = ndk_sys::AKEYCODE_BUTTON_Y, - ButtonZ = ndk_sys::AKEYCODE_BUTTON_Z, - ButtonL1 = ndk_sys::AKEYCODE_BUTTON_L1, - ButtonR1 = ndk_sys::AKEYCODE_BUTTON_R1, - ButtonL2 = ndk_sys::AKEYCODE_BUTTON_L2, - ButtonR2 = ndk_sys::AKEYCODE_BUTTON_R2, - ButtonThumbl = ndk_sys::AKEYCODE_BUTTON_THUMBL, - ButtonThumbr = ndk_sys::AKEYCODE_BUTTON_THUMBR, - ButtonStart = ndk_sys::AKEYCODE_BUTTON_START, - ButtonSelect = ndk_sys::AKEYCODE_BUTTON_SELECT, - ButtonMode = ndk_sys::AKEYCODE_BUTTON_MODE, - Escape = ndk_sys::AKEYCODE_ESCAPE, - ForwardDel = ndk_sys::AKEYCODE_FORWARD_DEL, - CtrlLeft = ndk_sys::AKEYCODE_CTRL_LEFT, - CtrlRight = ndk_sys::AKEYCODE_CTRL_RIGHT, - CapsLock = ndk_sys::AKEYCODE_CAPS_LOCK, - ScrollLock = ndk_sys::AKEYCODE_SCROLL_LOCK, - MetaLeft = ndk_sys::AKEYCODE_META_LEFT, - MetaRight = ndk_sys::AKEYCODE_META_RIGHT, - Function = ndk_sys::AKEYCODE_FUNCTION, - Sysrq = ndk_sys::AKEYCODE_SYSRQ, - Break = ndk_sys::AKEYCODE_BREAK, - MoveHome = ndk_sys::AKEYCODE_MOVE_HOME, - MoveEnd = ndk_sys::AKEYCODE_MOVE_END, - Insert = ndk_sys::AKEYCODE_INSERT, - Forward = ndk_sys::AKEYCODE_FORWARD, - MediaPlay = ndk_sys::AKEYCODE_MEDIA_PLAY, - MediaPause = ndk_sys::AKEYCODE_MEDIA_PAUSE, - MediaClose = ndk_sys::AKEYCODE_MEDIA_CLOSE, - MediaEject = ndk_sys::AKEYCODE_MEDIA_EJECT, - MediaRecord = ndk_sys::AKEYCODE_MEDIA_RECORD, - F1 = ndk_sys::AKEYCODE_F1, - F2 = ndk_sys::AKEYCODE_F2, - F3 = ndk_sys::AKEYCODE_F3, - F4 = ndk_sys::AKEYCODE_F4, - F5 = ndk_sys::AKEYCODE_F5, - F6 = ndk_sys::AKEYCODE_F6, - F7 = ndk_sys::AKEYCODE_F7, - F8 = ndk_sys::AKEYCODE_F8, - F9 = ndk_sys::AKEYCODE_F9, - F10 = ndk_sys::AKEYCODE_F10, - F11 = ndk_sys::AKEYCODE_F11, - F12 = ndk_sys::AKEYCODE_F12, - NumLock = ndk_sys::AKEYCODE_NUM_LOCK, - Numpad0 = ndk_sys::AKEYCODE_NUMPAD_0, - Numpad1 = ndk_sys::AKEYCODE_NUMPAD_1, - Numpad2 = ndk_sys::AKEYCODE_NUMPAD_2, - Numpad3 = ndk_sys::AKEYCODE_NUMPAD_3, - Numpad4 = ndk_sys::AKEYCODE_NUMPAD_4, - Numpad5 = ndk_sys::AKEYCODE_NUMPAD_5, - Numpad6 = ndk_sys::AKEYCODE_NUMPAD_6, - Numpad7 = ndk_sys::AKEYCODE_NUMPAD_7, - Numpad8 = ndk_sys::AKEYCODE_NUMPAD_8, - Numpad9 = ndk_sys::AKEYCODE_NUMPAD_9, - NumpadDivide = ndk_sys::AKEYCODE_NUMPAD_DIVIDE, - NumpadMultiply = ndk_sys::AKEYCODE_NUMPAD_MULTIPLY, - NumpadSubtract = ndk_sys::AKEYCODE_NUMPAD_SUBTRACT, - NumpadAdd = ndk_sys::AKEYCODE_NUMPAD_ADD, - NumpadDot = ndk_sys::AKEYCODE_NUMPAD_DOT, - NumpadComma = ndk_sys::AKEYCODE_NUMPAD_COMMA, - NumpadEnter = ndk_sys::AKEYCODE_NUMPAD_ENTER, - NumpadEquals = ndk_sys::AKEYCODE_NUMPAD_EQUALS, - NumpadLeftParen = ndk_sys::AKEYCODE_NUMPAD_LEFT_PAREN, - NumpadRightParen = ndk_sys::AKEYCODE_NUMPAD_RIGHT_PAREN, - VolumeMute = ndk_sys::AKEYCODE_VOLUME_MUTE, - Info = ndk_sys::AKEYCODE_INFO, - ChannelUp = ndk_sys::AKEYCODE_CHANNEL_UP, - ChannelDown = ndk_sys::AKEYCODE_CHANNEL_DOWN, - ZoomIn = ndk_sys::AKEYCODE_ZOOM_IN, - ZoomOut = ndk_sys::AKEYCODE_ZOOM_OUT, - Tv = ndk_sys::AKEYCODE_TV, - Window = ndk_sys::AKEYCODE_WINDOW, - Guide = ndk_sys::AKEYCODE_GUIDE, - Dvr = ndk_sys::AKEYCODE_DVR, - Bookmark = ndk_sys::AKEYCODE_BOOKMARK, - Captions = ndk_sys::AKEYCODE_CAPTIONS, - Settings = ndk_sys::AKEYCODE_SETTINGS, - TvPower = ndk_sys::AKEYCODE_TV_POWER, - TvInput = ndk_sys::AKEYCODE_TV_INPUT, - StbPower = ndk_sys::AKEYCODE_STB_POWER, - StbInput = ndk_sys::AKEYCODE_STB_INPUT, - AvrPower = ndk_sys::AKEYCODE_AVR_POWER, - AvrInput = ndk_sys::AKEYCODE_AVR_INPUT, - ProgRed = ndk_sys::AKEYCODE_PROG_RED, - ProgGreen = ndk_sys::AKEYCODE_PROG_GREEN, - ProgYellow = ndk_sys::AKEYCODE_PROG_YELLOW, - ProgBlue = ndk_sys::AKEYCODE_PROG_BLUE, - AppSwitch = ndk_sys::AKEYCODE_APP_SWITCH, - Button1 = ndk_sys::AKEYCODE_BUTTON_1, - Button2 = ndk_sys::AKEYCODE_BUTTON_2, - Button3 = ndk_sys::AKEYCODE_BUTTON_3, - Button4 = ndk_sys::AKEYCODE_BUTTON_4, - Button5 = ndk_sys::AKEYCODE_BUTTON_5, - Button6 = ndk_sys::AKEYCODE_BUTTON_6, - Button7 = ndk_sys::AKEYCODE_BUTTON_7, - Button8 = ndk_sys::AKEYCODE_BUTTON_8, - Button9 = ndk_sys::AKEYCODE_BUTTON_9, - Button10 = ndk_sys::AKEYCODE_BUTTON_10, - Button11 = ndk_sys::AKEYCODE_BUTTON_11, - Button12 = ndk_sys::AKEYCODE_BUTTON_12, - Button13 = ndk_sys::AKEYCODE_BUTTON_13, - Button14 = ndk_sys::AKEYCODE_BUTTON_14, - Button15 = ndk_sys::AKEYCODE_BUTTON_15, - Button16 = ndk_sys::AKEYCODE_BUTTON_16, - LanguageSwitch = ndk_sys::AKEYCODE_LANGUAGE_SWITCH, - MannerMode = ndk_sys::AKEYCODE_MANNER_MODE, - Keycode3dMode = ndk_sys::AKEYCODE_3D_MODE, - Contacts = ndk_sys::AKEYCODE_CONTACTS, - Calendar = ndk_sys::AKEYCODE_CALENDAR, - Music = ndk_sys::AKEYCODE_MUSIC, - Calculator = ndk_sys::AKEYCODE_CALCULATOR, - ZenkakuHankaku = ndk_sys::AKEYCODE_ZENKAKU_HANKAKU, - Eisu = ndk_sys::AKEYCODE_EISU, - Muhenkan = ndk_sys::AKEYCODE_MUHENKAN, - Henkan = ndk_sys::AKEYCODE_HENKAN, - KatakanaHiragana = ndk_sys::AKEYCODE_KATAKANA_HIRAGANA, - Yen = ndk_sys::AKEYCODE_YEN, - Ro = ndk_sys::AKEYCODE_RO, - Kana = ndk_sys::AKEYCODE_KANA, - Assist = ndk_sys::AKEYCODE_ASSIST, - BrightnessDown = ndk_sys::AKEYCODE_BRIGHTNESS_DOWN, - BrightnessUp = ndk_sys::AKEYCODE_BRIGHTNESS_UP, - MediaAudioTrack = ndk_sys::AKEYCODE_MEDIA_AUDIO_TRACK, - Sleep = ndk_sys::AKEYCODE_SLEEP, - Wakeup = ndk_sys::AKEYCODE_WAKEUP, - Pairing = ndk_sys::AKEYCODE_PAIRING, - MediaTopMenu = ndk_sys::AKEYCODE_MEDIA_TOP_MENU, - Keycode11 = ndk_sys::AKEYCODE_11, - Keycode12 = ndk_sys::AKEYCODE_12, - LastChannel = ndk_sys::AKEYCODE_LAST_CHANNEL, - TvDataService = ndk_sys::AKEYCODE_TV_DATA_SERVICE, - VoiceAssist = ndk_sys::AKEYCODE_VOICE_ASSIST, - TvRadioService = ndk_sys::AKEYCODE_TV_RADIO_SERVICE, - TvTeletext = ndk_sys::AKEYCODE_TV_TELETEXT, - TvNumberEntry = ndk_sys::AKEYCODE_TV_NUMBER_ENTRY, - TvTerrestrialAnalog = ndk_sys::AKEYCODE_TV_TERRESTRIAL_ANALOG, - TvTerrestrialDigital = ndk_sys::AKEYCODE_TV_TERRESTRIAL_DIGITAL, - TvSatellite = ndk_sys::AKEYCODE_TV_SATELLITE, - TvSatelliteBs = ndk_sys::AKEYCODE_TV_SATELLITE_BS, - TvSatelliteCs = ndk_sys::AKEYCODE_TV_SATELLITE_CS, - TvSatelliteService = ndk_sys::AKEYCODE_TV_SATELLITE_SERVICE, - TvNetwork = ndk_sys::AKEYCODE_TV_NETWORK, - TvAntennaCable = ndk_sys::AKEYCODE_TV_ANTENNA_CABLE, - TvInputHdmi1 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_1, - TvInputHdmi2 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_2, - TvInputHdmi3 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_3, - TvInputHdmi4 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_4, - TvInputComposite1 = ndk_sys::AKEYCODE_TV_INPUT_COMPOSITE_1, - TvInputComposite2 = ndk_sys::AKEYCODE_TV_INPUT_COMPOSITE_2, - TvInputComponent1 = ndk_sys::AKEYCODE_TV_INPUT_COMPONENT_1, - TvInputComponent2 = ndk_sys::AKEYCODE_TV_INPUT_COMPONENT_2, - TvInputVga1 = ndk_sys::AKEYCODE_TV_INPUT_VGA_1, - TvAudioDescription = ndk_sys::AKEYCODE_TV_AUDIO_DESCRIPTION, - TvAudioDescriptionMixUp = ndk_sys::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP, - TvAudioDescriptionMixDown = ndk_sys::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN, - TvZoomMode = ndk_sys::AKEYCODE_TV_ZOOM_MODE, - TvContentsMenu = ndk_sys::AKEYCODE_TV_CONTENTS_MENU, - TvMediaContextMenu = ndk_sys::AKEYCODE_TV_MEDIA_CONTEXT_MENU, - TvTimerProgramming = ndk_sys::AKEYCODE_TV_TIMER_PROGRAMMING, - Help = ndk_sys::AKEYCODE_HELP, - NavigatePrevious = ndk_sys::AKEYCODE_NAVIGATE_PREVIOUS, - NavigateNext = ndk_sys::AKEYCODE_NAVIGATE_NEXT, - NavigateIn = ndk_sys::AKEYCODE_NAVIGATE_IN, - NavigateOut = ndk_sys::AKEYCODE_NAVIGATE_OUT, - StemPrimary = ndk_sys::AKEYCODE_STEM_PRIMARY, - Stem1 = ndk_sys::AKEYCODE_STEM_1, - Stem2 = ndk_sys::AKEYCODE_STEM_2, - Stem3 = ndk_sys::AKEYCODE_STEM_3, - DpadUpLeft = ndk_sys::AKEYCODE_DPAD_UP_LEFT, - DpadDownLeft = ndk_sys::AKEYCODE_DPAD_DOWN_LEFT, - DpadUpRight = ndk_sys::AKEYCODE_DPAD_UP_RIGHT, - DpadDownRight = ndk_sys::AKEYCODE_DPAD_DOWN_RIGHT, - MediaSkipForward = ndk_sys::AKEYCODE_MEDIA_SKIP_FORWARD, - MediaSkipBackward = ndk_sys::AKEYCODE_MEDIA_SKIP_BACKWARD, - MediaStepForward = ndk_sys::AKEYCODE_MEDIA_STEP_FORWARD, - MediaStepBackward = ndk_sys::AKEYCODE_MEDIA_STEP_BACKWARD, - SoftSleep = ndk_sys::AKEYCODE_SOFT_SLEEP, - Cut = ndk_sys::AKEYCODE_CUT, - Copy = ndk_sys::AKEYCODE_COPY, - Paste = ndk_sys::AKEYCODE_PASTE, - SystemNavigationUp = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_UP, - SystemNavigationDown = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_DOWN, - SystemNavigationLeft = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_LEFT, - SystemNavigationRight = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_RIGHT, - AllApps = ndk_sys::AKEYCODE_ALL_APPS, - Refresh = ndk_sys::AKEYCODE_REFRESH, - ThumbsUp = ndk_sys::AKEYCODE_THUMBS_UP, - ThumbsDown = ndk_sys::AKEYCODE_THUMBS_DOWN, - ProfileSwitch = ndk_sys::AKEYCODE_PROFILE_SWITCH, -} - impl<'a> KeyEvent<'a> { pub(crate) fn new(ga_event: &'a GameActivityKeyEvent) -> Self { Self { ga_event } @@ -1329,59 +782,6 @@ impl<'a> KeyEvent<'a> { } } -/// Flags associated with [`KeyEvent`]. -/// -/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-28) -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct KeyEventFlags(pub u32); - -impl KeyEventFlags { - #[inline] - pub fn cancelled(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_CANCELED != 0 - } - #[inline] - pub fn cancelled_long_press(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_CANCELED_LONG_PRESS != 0 - } - #[inline] - pub fn editor_action(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_EDITOR_ACTION != 0 - } - #[inline] - pub fn fallback(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_FALLBACK != 0 - } - #[inline] - pub fn from_system(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_FROM_SYSTEM != 0 - } - #[inline] - pub fn keep_touch_mode(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_KEEP_TOUCH_MODE != 0 - } - #[inline] - pub fn long_press(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_LONG_PRESS != 0 - } - #[inline] - pub fn soft_keyboard(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_SOFT_KEYBOARD != 0 - } - #[inline] - pub fn tracking(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_TRACKING != 0 - } - #[inline] - pub fn virtual_hard_key(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY != 0 - } - #[inline] - pub fn woke_here(&self) -> bool { - self.0 & ndk_sys::AKEY_EVENT_FLAG_WOKE_HERE != 0 - } -} - impl<'a> KeyEvent<'a> { /// Flags associated with this [`KeyEvent`]. /// diff --git a/android-activity/src/game_activity/mod.rs b/android-activity/src/game_activity/mod.rs index 88a9fd6..02b3e37 100644 --- a/android-activity/src/game_activity/mod.rs +++ b/android-activity/src/game_activity/mod.rs @@ -27,7 +27,7 @@ use ndk::configuration::Configuration; use ndk::native_window::NativeWindow; use crate::error::InternalResult; -use crate::input::{KeyCharacterMap, KeyCharacterMapBinding}; +use crate::input::{Axis, KeyCharacterMap, KeyCharacterMapBinding}; use crate::jni_utils::{self, CloneJavaVM}; use crate::util::{abort_on_panic, android_log, log_panic}; use crate::{ @@ -38,7 +38,7 @@ mod ffi; pub mod input; use crate::input::{TextInputState, TextSpan}; -use input::{Axis, InputEvent, KeyEvent, MotionEvent}; +use input::{InputEvent, KeyEvent, MotionEvent}; // The only time it's safe to update the android_app->savedState pointer is // while handling a SaveState event, so this API is only exposed for those diff --git a/android-activity/src/input.rs b/android-activity/src/input.rs index f62a088..743244f 100644 --- a/android-activity/src/input.rs +++ b/android-activity/src/input.rs @@ -87,6 +87,679 @@ impl From for Class { } } +/// A bitfield representing the state of modifier keys during an event. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-25) +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct MetaState(pub u32); + +impl MetaState { + #[inline] + pub fn alt_on(self) -> bool { + self.0 & ndk_sys::AMETA_ALT_ON != 0 + } + #[inline] + pub fn alt_left_on(self) -> bool { + self.0 & ndk_sys::AMETA_ALT_LEFT_ON != 0 + } + #[inline] + pub fn alt_right_on(self) -> bool { + self.0 & ndk_sys::AMETA_ALT_RIGHT_ON != 0 + } + #[inline] + pub fn shift_on(self) -> bool { + self.0 & ndk_sys::AMETA_SHIFT_ON != 0 + } + #[inline] + pub fn shift_left_on(self) -> bool { + self.0 & ndk_sys::AMETA_SHIFT_LEFT_ON != 0 + } + #[inline] + pub fn shift_right_on(self) -> bool { + self.0 & ndk_sys::AMETA_SHIFT_RIGHT_ON != 0 + } + #[inline] + pub fn sym_on(self) -> bool { + self.0 & ndk_sys::AMETA_SYM_ON != 0 + } + #[inline] + pub fn function_on(self) -> bool { + self.0 & ndk_sys::AMETA_FUNCTION_ON != 0 + } + #[inline] + pub fn ctrl_on(self) -> bool { + self.0 & ndk_sys::AMETA_CTRL_ON != 0 + } + #[inline] + pub fn ctrl_left_on(self) -> bool { + self.0 & ndk_sys::AMETA_CTRL_LEFT_ON != 0 + } + #[inline] + pub fn ctrl_right_on(self) -> bool { + self.0 & ndk_sys::AMETA_CTRL_RIGHT_ON != 0 + } + #[inline] + pub fn meta_on(self) -> bool { + self.0 & ndk_sys::AMETA_META_ON != 0 + } + #[inline] + pub fn meta_left_on(self) -> bool { + self.0 & ndk_sys::AMETA_META_LEFT_ON != 0 + } + #[inline] + pub fn meta_right_on(self) -> bool { + self.0 & ndk_sys::AMETA_META_RIGHT_ON != 0 + } + #[inline] + pub fn caps_lock_on(self) -> bool { + self.0 & ndk_sys::AMETA_CAPS_LOCK_ON != 0 + } + #[inline] + pub fn num_lock_on(self) -> bool { + self.0 & ndk_sys::AMETA_NUM_LOCK_ON != 0 + } + #[inline] + pub fn scroll_lock_on(self) -> bool { + self.0 & ndk_sys::AMETA_SCROLL_LOCK_ON != 0 + } +} + +impl From for MetaState { + fn from(value: ndk::event::MetaState) -> Self { + Self(value.0) + } +} + +/// A motion action. +/// +/// See [the NDK +/// docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-29) +#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] +#[repr(u32)] +pub enum MotionAction { + Down = ndk_sys::AMOTION_EVENT_ACTION_DOWN, + Up = ndk_sys::AMOTION_EVENT_ACTION_UP, + Move = ndk_sys::AMOTION_EVENT_ACTION_MOVE, + Cancel = ndk_sys::AMOTION_EVENT_ACTION_CANCEL, + Outside = ndk_sys::AMOTION_EVENT_ACTION_OUTSIDE, + PointerDown = ndk_sys::AMOTION_EVENT_ACTION_POINTER_DOWN, + PointerUp = ndk_sys::AMOTION_EVENT_ACTION_POINTER_UP, + HoverMove = ndk_sys::AMOTION_EVENT_ACTION_HOVER_MOVE, + Scroll = ndk_sys::AMOTION_EVENT_ACTION_SCROLL, + HoverEnter = ndk_sys::AMOTION_EVENT_ACTION_HOVER_ENTER, + HoverExit = ndk_sys::AMOTION_EVENT_ACTION_HOVER_EXIT, + ButtonPress = ndk_sys::AMOTION_EVENT_ACTION_BUTTON_PRESS, + ButtonRelease = ndk_sys::AMOTION_EVENT_ACTION_BUTTON_RELEASE, +} + +impl From for MotionAction { + fn from(value: ndk::event::MotionAction) -> Self { + let inner: u32 = value.into(); + inner.try_into().unwrap() // TODO: use into() when we bump the MSRV to 1.68 + } +} + +/// An axis of a motion event. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-32) +#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] +#[repr(u32)] +pub enum Axis { + X = ndk_sys::AMOTION_EVENT_AXIS_X, + Y = ndk_sys::AMOTION_EVENT_AXIS_Y, + Pressure = ndk_sys::AMOTION_EVENT_AXIS_PRESSURE, + Size = ndk_sys::AMOTION_EVENT_AXIS_SIZE, + TouchMajor = ndk_sys::AMOTION_EVENT_AXIS_TOUCH_MAJOR, + TouchMinor = ndk_sys::AMOTION_EVENT_AXIS_TOUCH_MINOR, + ToolMajor = ndk_sys::AMOTION_EVENT_AXIS_TOOL_MAJOR, + ToolMinor = ndk_sys::AMOTION_EVENT_AXIS_TOOL_MINOR, + Orientation = ndk_sys::AMOTION_EVENT_AXIS_ORIENTATION, + Vscroll = ndk_sys::AMOTION_EVENT_AXIS_VSCROLL, + Hscroll = ndk_sys::AMOTION_EVENT_AXIS_HSCROLL, + Z = ndk_sys::AMOTION_EVENT_AXIS_Z, + Rx = ndk_sys::AMOTION_EVENT_AXIS_RX, + Ry = ndk_sys::AMOTION_EVENT_AXIS_RY, + Rz = ndk_sys::AMOTION_EVENT_AXIS_RZ, + HatX = ndk_sys::AMOTION_EVENT_AXIS_HAT_X, + HatY = ndk_sys::AMOTION_EVENT_AXIS_HAT_Y, + Ltrigger = ndk_sys::AMOTION_EVENT_AXIS_LTRIGGER, + Rtrigger = ndk_sys::AMOTION_EVENT_AXIS_RTRIGGER, + Throttle = ndk_sys::AMOTION_EVENT_AXIS_THROTTLE, + Rudder = ndk_sys::AMOTION_EVENT_AXIS_RUDDER, + Wheel = ndk_sys::AMOTION_EVENT_AXIS_WHEEL, + Gas = ndk_sys::AMOTION_EVENT_AXIS_GAS, + Brake = ndk_sys::AMOTION_EVENT_AXIS_BRAKE, + Distance = ndk_sys::AMOTION_EVENT_AXIS_DISTANCE, + Tilt = ndk_sys::AMOTION_EVENT_AXIS_TILT, + Scroll = ndk_sys::AMOTION_EVENT_AXIS_SCROLL, + RelativeX = ndk_sys::AMOTION_EVENT_AXIS_RELATIVE_X, + RelativeY = ndk_sys::AMOTION_EVENT_AXIS_RELATIVE_Y, + Generic1 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_1, + Generic2 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_2, + Generic3 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_3, + Generic4 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_4, + Generic5 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_5, + Generic6 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_6, + Generic7 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_7, + Generic8 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_8, + Generic9 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_9, + Generic10 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_10, + Generic11 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_11, + Generic12 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_12, + Generic13 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_13, + Generic14 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_14, + Generic15 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_15, + Generic16 = ndk_sys::AMOTION_EVENT_AXIS_GENERIC_16, +} + +impl From for Axis { + fn from(value: ndk::event::Axis) -> Self { + let inner: u32 = value.into(); + inner.try_into().unwrap() // TODO: replace with into() when we can bump MSRV to 1.68! + } +} + +/// The tool type of a pointer. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-48) +#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] +#[repr(u32)] +pub enum ToolType { + /// Unknown tool type. + /// + /// This constant is used when the tool type is not known or is not relevant, such as for a trackball or other non-pointing device. + Unknown = ndk_sys::AMOTION_EVENT_TOOL_TYPE_UNKNOWN, + + /// The tool is a finger. + Finger = ndk_sys::AMOTION_EVENT_TOOL_TYPE_FINGER, + + /// The tool is a stylus. + Stylus = ndk_sys::AMOTION_EVENT_TOOL_TYPE_STYLUS, + + /// The tool is a mouse. + Mouse = ndk_sys::AMOTION_EVENT_TOOL_TYPE_MOUSE, + + /// The tool is an eraser or a stylus being used in an inverted posture. + Eraser = ndk_sys::AMOTION_EVENT_TOOL_TYPE_ERASER, + + /// The tool is a palm and should be rejected + Palm = ndk_sys::AMOTION_EVENT_TOOL_TYPE_PALM, +} + +/// A bitfield representing the state of buttons during a motion event. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-33) +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct ButtonState(pub u32); + +impl ButtonState { + #[inline] + pub fn primary(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_BUTTON_PRIMARY != 0 + } + #[inline] + pub fn secondary(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_BUTTON_SECONDARY != 0 + } + #[inline] + pub fn teriary(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_BUTTON_TERTIARY != 0 + } + #[inline] + pub fn back(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_BUTTON_BACK != 0 + } + #[inline] + pub fn forward(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_BUTTON_FORWARD != 0 + } + #[inline] + pub fn stylus_primary(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_PRIMARY != 0 + } + #[inline] + pub fn stylus_secondary(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_BUTTON_STYLUS_SECONDARY != 0 + } +} + +impl From for ButtonState { + fn from(value: ndk::event::ButtonState) -> Self { + Self(value.0) + } +} + +/// A bitfield representing which edges were touched by a motion event. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-31) +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct EdgeFlags(pub u32); + +impl EdgeFlags { + #[inline] + pub fn top(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_TOP != 0 + } + #[inline] + pub fn bottom(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_BOTTOM != 0 + } + #[inline] + pub fn left(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_LEFT != 0 + } + #[inline] + pub fn right(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_EDGE_FLAG_RIGHT != 0 + } +} + +impl From for EdgeFlags { + fn from(value: ndk::event::EdgeFlags) -> Self { + Self(value.0) + } +} + +/// Flags associated with this [`MotionEvent`]. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-30) +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct MotionEventFlags(pub u32); + +impl MotionEventFlags { + #[inline] + pub fn window_is_obscured(self) -> bool { + self.0 & ndk_sys::AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED != 0 + } +} + +impl From for MotionEventFlags { + fn from(value: ndk::event::MotionEventFlags) -> Self { + Self(value.0) + } +} + +/// Key actions. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-27) +#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] +#[repr(u32)] +pub enum KeyAction { + Down = ndk_sys::AKEY_EVENT_ACTION_DOWN, + Up = ndk_sys::AKEY_EVENT_ACTION_UP, + Multiple = ndk_sys::AKEY_EVENT_ACTION_MULTIPLE, +} + +impl From for KeyAction { + fn from(value: ndk::event::KeyAction) -> Self { + let inner: u32 = value.into(); + inner.try_into().unwrap() // TODO: replace with into() when we can bump MSRV to 1.68! + } +} + +/// Key codes. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-39) +#[derive(Copy, Clone, Debug, PartialEq, Eq, TryFromPrimitive, IntoPrimitive)] +#[repr(u32)] +pub enum Keycode { + Unknown = ndk_sys::AKEYCODE_UNKNOWN, + SoftLeft = ndk_sys::AKEYCODE_SOFT_LEFT, + SoftRight = ndk_sys::AKEYCODE_SOFT_RIGHT, + Home = ndk_sys::AKEYCODE_HOME, + Back = ndk_sys::AKEYCODE_BACK, + Call = ndk_sys::AKEYCODE_CALL, + Endcall = ndk_sys::AKEYCODE_ENDCALL, + Keycode0 = ndk_sys::AKEYCODE_0, + Keycode1 = ndk_sys::AKEYCODE_1, + Keycode2 = ndk_sys::AKEYCODE_2, + Keycode3 = ndk_sys::AKEYCODE_3, + Keycode4 = ndk_sys::AKEYCODE_4, + Keycode5 = ndk_sys::AKEYCODE_5, + Keycode6 = ndk_sys::AKEYCODE_6, + Keycode7 = ndk_sys::AKEYCODE_7, + Keycode8 = ndk_sys::AKEYCODE_8, + Keycode9 = ndk_sys::AKEYCODE_9, + Star = ndk_sys::AKEYCODE_STAR, + Pound = ndk_sys::AKEYCODE_POUND, + DpadUp = ndk_sys::AKEYCODE_DPAD_UP, + DpadDown = ndk_sys::AKEYCODE_DPAD_DOWN, + DpadLeft = ndk_sys::AKEYCODE_DPAD_LEFT, + DpadRight = ndk_sys::AKEYCODE_DPAD_RIGHT, + DpadCenter = ndk_sys::AKEYCODE_DPAD_CENTER, + VolumeUp = ndk_sys::AKEYCODE_VOLUME_UP, + VolumeDown = ndk_sys::AKEYCODE_VOLUME_DOWN, + Power = ndk_sys::AKEYCODE_POWER, + Camera = ndk_sys::AKEYCODE_CAMERA, + Clear = ndk_sys::AKEYCODE_CLEAR, + A = ndk_sys::AKEYCODE_A, + B = ndk_sys::AKEYCODE_B, + C = ndk_sys::AKEYCODE_C, + D = ndk_sys::AKEYCODE_D, + E = ndk_sys::AKEYCODE_E, + F = ndk_sys::AKEYCODE_F, + G = ndk_sys::AKEYCODE_G, + H = ndk_sys::AKEYCODE_H, + I = ndk_sys::AKEYCODE_I, + J = ndk_sys::AKEYCODE_J, + K = ndk_sys::AKEYCODE_K, + L = ndk_sys::AKEYCODE_L, + M = ndk_sys::AKEYCODE_M, + N = ndk_sys::AKEYCODE_N, + O = ndk_sys::AKEYCODE_O, + P = ndk_sys::AKEYCODE_P, + Q = ndk_sys::AKEYCODE_Q, + R = ndk_sys::AKEYCODE_R, + S = ndk_sys::AKEYCODE_S, + T = ndk_sys::AKEYCODE_T, + U = ndk_sys::AKEYCODE_U, + V = ndk_sys::AKEYCODE_V, + W = ndk_sys::AKEYCODE_W, + X = ndk_sys::AKEYCODE_X, + Y = ndk_sys::AKEYCODE_Y, + Z = ndk_sys::AKEYCODE_Z, + Comma = ndk_sys::AKEYCODE_COMMA, + Period = ndk_sys::AKEYCODE_PERIOD, + AltLeft = ndk_sys::AKEYCODE_ALT_LEFT, + AltRight = ndk_sys::AKEYCODE_ALT_RIGHT, + ShiftLeft = ndk_sys::AKEYCODE_SHIFT_LEFT, + ShiftRight = ndk_sys::AKEYCODE_SHIFT_RIGHT, + Tab = ndk_sys::AKEYCODE_TAB, + Space = ndk_sys::AKEYCODE_SPACE, + Sym = ndk_sys::AKEYCODE_SYM, + Explorer = ndk_sys::AKEYCODE_EXPLORER, + Envelope = ndk_sys::AKEYCODE_ENVELOPE, + Enter = ndk_sys::AKEYCODE_ENTER, + Del = ndk_sys::AKEYCODE_DEL, + Grave = ndk_sys::AKEYCODE_GRAVE, + Minus = ndk_sys::AKEYCODE_MINUS, + Equals = ndk_sys::AKEYCODE_EQUALS, + LeftBracket = ndk_sys::AKEYCODE_LEFT_BRACKET, + RightBracket = ndk_sys::AKEYCODE_RIGHT_BRACKET, + Backslash = ndk_sys::AKEYCODE_BACKSLASH, + Semicolon = ndk_sys::AKEYCODE_SEMICOLON, + Apostrophe = ndk_sys::AKEYCODE_APOSTROPHE, + Slash = ndk_sys::AKEYCODE_SLASH, + At = ndk_sys::AKEYCODE_AT, + Num = ndk_sys::AKEYCODE_NUM, + Headsethook = ndk_sys::AKEYCODE_HEADSETHOOK, + Focus = ndk_sys::AKEYCODE_FOCUS, + Plus = ndk_sys::AKEYCODE_PLUS, + Menu = ndk_sys::AKEYCODE_MENU, + Notification = ndk_sys::AKEYCODE_NOTIFICATION, + Search = ndk_sys::AKEYCODE_SEARCH, + MediaPlayPause = ndk_sys::AKEYCODE_MEDIA_PLAY_PAUSE, + MediaStop = ndk_sys::AKEYCODE_MEDIA_STOP, + MediaNext = ndk_sys::AKEYCODE_MEDIA_NEXT, + MediaPrevious = ndk_sys::AKEYCODE_MEDIA_PREVIOUS, + MediaRewind = ndk_sys::AKEYCODE_MEDIA_REWIND, + MediaFastForward = ndk_sys::AKEYCODE_MEDIA_FAST_FORWARD, + Mute = ndk_sys::AKEYCODE_MUTE, + PageUp = ndk_sys::AKEYCODE_PAGE_UP, + PageDown = ndk_sys::AKEYCODE_PAGE_DOWN, + Pictsymbols = ndk_sys::AKEYCODE_PICTSYMBOLS, + SwitchCharset = ndk_sys::AKEYCODE_SWITCH_CHARSET, + ButtonA = ndk_sys::AKEYCODE_BUTTON_A, + ButtonB = ndk_sys::AKEYCODE_BUTTON_B, + ButtonC = ndk_sys::AKEYCODE_BUTTON_C, + ButtonX = ndk_sys::AKEYCODE_BUTTON_X, + ButtonY = ndk_sys::AKEYCODE_BUTTON_Y, + ButtonZ = ndk_sys::AKEYCODE_BUTTON_Z, + ButtonL1 = ndk_sys::AKEYCODE_BUTTON_L1, + ButtonR1 = ndk_sys::AKEYCODE_BUTTON_R1, + ButtonL2 = ndk_sys::AKEYCODE_BUTTON_L2, + ButtonR2 = ndk_sys::AKEYCODE_BUTTON_R2, + ButtonThumbl = ndk_sys::AKEYCODE_BUTTON_THUMBL, + ButtonThumbr = ndk_sys::AKEYCODE_BUTTON_THUMBR, + ButtonStart = ndk_sys::AKEYCODE_BUTTON_START, + ButtonSelect = ndk_sys::AKEYCODE_BUTTON_SELECT, + ButtonMode = ndk_sys::AKEYCODE_BUTTON_MODE, + Escape = ndk_sys::AKEYCODE_ESCAPE, + ForwardDel = ndk_sys::AKEYCODE_FORWARD_DEL, + CtrlLeft = ndk_sys::AKEYCODE_CTRL_LEFT, + CtrlRight = ndk_sys::AKEYCODE_CTRL_RIGHT, + CapsLock = ndk_sys::AKEYCODE_CAPS_LOCK, + ScrollLock = ndk_sys::AKEYCODE_SCROLL_LOCK, + MetaLeft = ndk_sys::AKEYCODE_META_LEFT, + MetaRight = ndk_sys::AKEYCODE_META_RIGHT, + Function = ndk_sys::AKEYCODE_FUNCTION, + Sysrq = ndk_sys::AKEYCODE_SYSRQ, + Break = ndk_sys::AKEYCODE_BREAK, + MoveHome = ndk_sys::AKEYCODE_MOVE_HOME, + MoveEnd = ndk_sys::AKEYCODE_MOVE_END, + Insert = ndk_sys::AKEYCODE_INSERT, + Forward = ndk_sys::AKEYCODE_FORWARD, + MediaPlay = ndk_sys::AKEYCODE_MEDIA_PLAY, + MediaPause = ndk_sys::AKEYCODE_MEDIA_PAUSE, + MediaClose = ndk_sys::AKEYCODE_MEDIA_CLOSE, + MediaEject = ndk_sys::AKEYCODE_MEDIA_EJECT, + MediaRecord = ndk_sys::AKEYCODE_MEDIA_RECORD, + F1 = ndk_sys::AKEYCODE_F1, + F2 = ndk_sys::AKEYCODE_F2, + F3 = ndk_sys::AKEYCODE_F3, + F4 = ndk_sys::AKEYCODE_F4, + F5 = ndk_sys::AKEYCODE_F5, + F6 = ndk_sys::AKEYCODE_F6, + F7 = ndk_sys::AKEYCODE_F7, + F8 = ndk_sys::AKEYCODE_F8, + F9 = ndk_sys::AKEYCODE_F9, + F10 = ndk_sys::AKEYCODE_F10, + F11 = ndk_sys::AKEYCODE_F11, + F12 = ndk_sys::AKEYCODE_F12, + NumLock = ndk_sys::AKEYCODE_NUM_LOCK, + Numpad0 = ndk_sys::AKEYCODE_NUMPAD_0, + Numpad1 = ndk_sys::AKEYCODE_NUMPAD_1, + Numpad2 = ndk_sys::AKEYCODE_NUMPAD_2, + Numpad3 = ndk_sys::AKEYCODE_NUMPAD_3, + Numpad4 = ndk_sys::AKEYCODE_NUMPAD_4, + Numpad5 = ndk_sys::AKEYCODE_NUMPAD_5, + Numpad6 = ndk_sys::AKEYCODE_NUMPAD_6, + Numpad7 = ndk_sys::AKEYCODE_NUMPAD_7, + Numpad8 = ndk_sys::AKEYCODE_NUMPAD_8, + Numpad9 = ndk_sys::AKEYCODE_NUMPAD_9, + NumpadDivide = ndk_sys::AKEYCODE_NUMPAD_DIVIDE, + NumpadMultiply = ndk_sys::AKEYCODE_NUMPAD_MULTIPLY, + NumpadSubtract = ndk_sys::AKEYCODE_NUMPAD_SUBTRACT, + NumpadAdd = ndk_sys::AKEYCODE_NUMPAD_ADD, + NumpadDot = ndk_sys::AKEYCODE_NUMPAD_DOT, + NumpadComma = ndk_sys::AKEYCODE_NUMPAD_COMMA, + NumpadEnter = ndk_sys::AKEYCODE_NUMPAD_ENTER, + NumpadEquals = ndk_sys::AKEYCODE_NUMPAD_EQUALS, + NumpadLeftParen = ndk_sys::AKEYCODE_NUMPAD_LEFT_PAREN, + NumpadRightParen = ndk_sys::AKEYCODE_NUMPAD_RIGHT_PAREN, + VolumeMute = ndk_sys::AKEYCODE_VOLUME_MUTE, + Info = ndk_sys::AKEYCODE_INFO, + ChannelUp = ndk_sys::AKEYCODE_CHANNEL_UP, + ChannelDown = ndk_sys::AKEYCODE_CHANNEL_DOWN, + ZoomIn = ndk_sys::AKEYCODE_ZOOM_IN, + ZoomOut = ndk_sys::AKEYCODE_ZOOM_OUT, + Tv = ndk_sys::AKEYCODE_TV, + Window = ndk_sys::AKEYCODE_WINDOW, + Guide = ndk_sys::AKEYCODE_GUIDE, + Dvr = ndk_sys::AKEYCODE_DVR, + Bookmark = ndk_sys::AKEYCODE_BOOKMARK, + Captions = ndk_sys::AKEYCODE_CAPTIONS, + Settings = ndk_sys::AKEYCODE_SETTINGS, + TvPower = ndk_sys::AKEYCODE_TV_POWER, + TvInput = ndk_sys::AKEYCODE_TV_INPUT, + StbPower = ndk_sys::AKEYCODE_STB_POWER, + StbInput = ndk_sys::AKEYCODE_STB_INPUT, + AvrPower = ndk_sys::AKEYCODE_AVR_POWER, + AvrInput = ndk_sys::AKEYCODE_AVR_INPUT, + ProgRed = ndk_sys::AKEYCODE_PROG_RED, + ProgGreen = ndk_sys::AKEYCODE_PROG_GREEN, + ProgYellow = ndk_sys::AKEYCODE_PROG_YELLOW, + ProgBlue = ndk_sys::AKEYCODE_PROG_BLUE, + AppSwitch = ndk_sys::AKEYCODE_APP_SWITCH, + Button1 = ndk_sys::AKEYCODE_BUTTON_1, + Button2 = ndk_sys::AKEYCODE_BUTTON_2, + Button3 = ndk_sys::AKEYCODE_BUTTON_3, + Button4 = ndk_sys::AKEYCODE_BUTTON_4, + Button5 = ndk_sys::AKEYCODE_BUTTON_5, + Button6 = ndk_sys::AKEYCODE_BUTTON_6, + Button7 = ndk_sys::AKEYCODE_BUTTON_7, + Button8 = ndk_sys::AKEYCODE_BUTTON_8, + Button9 = ndk_sys::AKEYCODE_BUTTON_9, + Button10 = ndk_sys::AKEYCODE_BUTTON_10, + Button11 = ndk_sys::AKEYCODE_BUTTON_11, + Button12 = ndk_sys::AKEYCODE_BUTTON_12, + Button13 = ndk_sys::AKEYCODE_BUTTON_13, + Button14 = ndk_sys::AKEYCODE_BUTTON_14, + Button15 = ndk_sys::AKEYCODE_BUTTON_15, + Button16 = ndk_sys::AKEYCODE_BUTTON_16, + LanguageSwitch = ndk_sys::AKEYCODE_LANGUAGE_SWITCH, + MannerMode = ndk_sys::AKEYCODE_MANNER_MODE, + Keycode3dMode = ndk_sys::AKEYCODE_3D_MODE, + Contacts = ndk_sys::AKEYCODE_CONTACTS, + Calendar = ndk_sys::AKEYCODE_CALENDAR, + Music = ndk_sys::AKEYCODE_MUSIC, + Calculator = ndk_sys::AKEYCODE_CALCULATOR, + ZenkakuHankaku = ndk_sys::AKEYCODE_ZENKAKU_HANKAKU, + Eisu = ndk_sys::AKEYCODE_EISU, + Muhenkan = ndk_sys::AKEYCODE_MUHENKAN, + Henkan = ndk_sys::AKEYCODE_HENKAN, + KatakanaHiragana = ndk_sys::AKEYCODE_KATAKANA_HIRAGANA, + Yen = ndk_sys::AKEYCODE_YEN, + Ro = ndk_sys::AKEYCODE_RO, + Kana = ndk_sys::AKEYCODE_KANA, + Assist = ndk_sys::AKEYCODE_ASSIST, + BrightnessDown = ndk_sys::AKEYCODE_BRIGHTNESS_DOWN, + BrightnessUp = ndk_sys::AKEYCODE_BRIGHTNESS_UP, + MediaAudioTrack = ndk_sys::AKEYCODE_MEDIA_AUDIO_TRACK, + Sleep = ndk_sys::AKEYCODE_SLEEP, + Wakeup = ndk_sys::AKEYCODE_WAKEUP, + Pairing = ndk_sys::AKEYCODE_PAIRING, + MediaTopMenu = ndk_sys::AKEYCODE_MEDIA_TOP_MENU, + Keycode11 = ndk_sys::AKEYCODE_11, + Keycode12 = ndk_sys::AKEYCODE_12, + LastChannel = ndk_sys::AKEYCODE_LAST_CHANNEL, + TvDataService = ndk_sys::AKEYCODE_TV_DATA_SERVICE, + VoiceAssist = ndk_sys::AKEYCODE_VOICE_ASSIST, + TvRadioService = ndk_sys::AKEYCODE_TV_RADIO_SERVICE, + TvTeletext = ndk_sys::AKEYCODE_TV_TELETEXT, + TvNumberEntry = ndk_sys::AKEYCODE_TV_NUMBER_ENTRY, + TvTerrestrialAnalog = ndk_sys::AKEYCODE_TV_TERRESTRIAL_ANALOG, + TvTerrestrialDigital = ndk_sys::AKEYCODE_TV_TERRESTRIAL_DIGITAL, + TvSatellite = ndk_sys::AKEYCODE_TV_SATELLITE, + TvSatelliteBs = ndk_sys::AKEYCODE_TV_SATELLITE_BS, + TvSatelliteCs = ndk_sys::AKEYCODE_TV_SATELLITE_CS, + TvSatelliteService = ndk_sys::AKEYCODE_TV_SATELLITE_SERVICE, + TvNetwork = ndk_sys::AKEYCODE_TV_NETWORK, + TvAntennaCable = ndk_sys::AKEYCODE_TV_ANTENNA_CABLE, + TvInputHdmi1 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_1, + TvInputHdmi2 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_2, + TvInputHdmi3 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_3, + TvInputHdmi4 = ndk_sys::AKEYCODE_TV_INPUT_HDMI_4, + TvInputComposite1 = ndk_sys::AKEYCODE_TV_INPUT_COMPOSITE_1, + TvInputComposite2 = ndk_sys::AKEYCODE_TV_INPUT_COMPOSITE_2, + TvInputComponent1 = ndk_sys::AKEYCODE_TV_INPUT_COMPONENT_1, + TvInputComponent2 = ndk_sys::AKEYCODE_TV_INPUT_COMPONENT_2, + TvInputVga1 = ndk_sys::AKEYCODE_TV_INPUT_VGA_1, + TvAudioDescription = ndk_sys::AKEYCODE_TV_AUDIO_DESCRIPTION, + TvAudioDescriptionMixUp = ndk_sys::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_UP, + TvAudioDescriptionMixDown = ndk_sys::AKEYCODE_TV_AUDIO_DESCRIPTION_MIX_DOWN, + TvZoomMode = ndk_sys::AKEYCODE_TV_ZOOM_MODE, + TvContentsMenu = ndk_sys::AKEYCODE_TV_CONTENTS_MENU, + TvMediaContextMenu = ndk_sys::AKEYCODE_TV_MEDIA_CONTEXT_MENU, + TvTimerProgramming = ndk_sys::AKEYCODE_TV_TIMER_PROGRAMMING, + Help = ndk_sys::AKEYCODE_HELP, + NavigatePrevious = ndk_sys::AKEYCODE_NAVIGATE_PREVIOUS, + NavigateNext = ndk_sys::AKEYCODE_NAVIGATE_NEXT, + NavigateIn = ndk_sys::AKEYCODE_NAVIGATE_IN, + NavigateOut = ndk_sys::AKEYCODE_NAVIGATE_OUT, + StemPrimary = ndk_sys::AKEYCODE_STEM_PRIMARY, + Stem1 = ndk_sys::AKEYCODE_STEM_1, + Stem2 = ndk_sys::AKEYCODE_STEM_2, + Stem3 = ndk_sys::AKEYCODE_STEM_3, + DpadUpLeft = ndk_sys::AKEYCODE_DPAD_UP_LEFT, + DpadDownLeft = ndk_sys::AKEYCODE_DPAD_DOWN_LEFT, + DpadUpRight = ndk_sys::AKEYCODE_DPAD_UP_RIGHT, + DpadDownRight = ndk_sys::AKEYCODE_DPAD_DOWN_RIGHT, + MediaSkipForward = ndk_sys::AKEYCODE_MEDIA_SKIP_FORWARD, + MediaSkipBackward = ndk_sys::AKEYCODE_MEDIA_SKIP_BACKWARD, + MediaStepForward = ndk_sys::AKEYCODE_MEDIA_STEP_FORWARD, + MediaStepBackward = ndk_sys::AKEYCODE_MEDIA_STEP_BACKWARD, + SoftSleep = ndk_sys::AKEYCODE_SOFT_SLEEP, + Cut = ndk_sys::AKEYCODE_CUT, + Copy = ndk_sys::AKEYCODE_COPY, + Paste = ndk_sys::AKEYCODE_PASTE, + SystemNavigationUp = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_UP, + SystemNavigationDown = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_DOWN, + SystemNavigationLeft = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_LEFT, + SystemNavigationRight = ndk_sys::AKEYCODE_SYSTEM_NAVIGATION_RIGHT, + AllApps = ndk_sys::AKEYCODE_ALL_APPS, + Refresh = ndk_sys::AKEYCODE_REFRESH, + ThumbsUp = ndk_sys::AKEYCODE_THUMBS_UP, + ThumbsDown = ndk_sys::AKEYCODE_THUMBS_DOWN, + ProfileSwitch = ndk_sys::AKEYCODE_PROFILE_SWITCH, +} + +impl From for Keycode { + fn from(value: ndk::event::Keycode) -> Self { + let inner: u32 = value.into(); + inner.try_into().unwrap() // TODO: replace with into() when we can bump MSRV to 1.68! + } +} + +/// Flags associated with [`KeyEvent`]. +/// +/// See [the NDK docs](https://developer.android.com/ndk/reference/group/input#anonymous-enum-28) +#[derive(Copy, Clone, Debug, PartialEq, Eq)] +pub struct KeyEventFlags(pub u32); + +impl KeyEventFlags { + #[inline] + pub fn cancelled(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_CANCELED != 0 + } + #[inline] + pub fn cancelled_long_press(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_CANCELED_LONG_PRESS != 0 + } + #[inline] + pub fn editor_action(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_EDITOR_ACTION != 0 + } + #[inline] + pub fn fallback(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_FALLBACK != 0 + } + #[inline] + pub fn from_system(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_FROM_SYSTEM != 0 + } + #[inline] + pub fn keep_touch_mode(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_KEEP_TOUCH_MODE != 0 + } + #[inline] + pub fn long_press(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_LONG_PRESS != 0 + } + #[inline] + pub fn soft_keyboard(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_SOFT_KEYBOARD != 0 + } + #[inline] + pub fn tracking(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_TRACKING != 0 + } + #[inline] + pub fn virtual_hard_key(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_VIRTUAL_HARD_KEY != 0 + } + #[inline] + pub fn woke_here(&self) -> bool { + self.0 & ndk_sys::AKEY_EVENT_FLAG_WOKE_HERE != 0 + } +} + +impl From for KeyEventFlags { + fn from(value: ndk::event::KeyEventFlags) -> Self { + Self(value.0) + } +} + /// This struct holds a span within a region of text from `start` to `end`. /// /// The `start` index may be greater than the `end` index (swapping `start` and `end` will represent the same span) diff --git a/android-activity/src/input/sdk.rs b/android-activity/src/input/sdk.rs index 4d1e84b..f03a41b 100644 --- a/android-activity/src/input/sdk.rs +++ b/android-activity/src/input/sdk.rs @@ -8,7 +8,7 @@ use jni::{ use jni_sys::jint; use crate::{ - activity_impl::input::{Keycode, MetaState}, + input::{Keycode, MetaState}, jni_utils::CloneJavaVM, }; diff --git a/android-activity/src/native_activity/input.rs b/android-activity/src/native_activity/input.rs index 2056fe0..ca32b7f 100644 --- a/android-activity/src/native_activity/input.rs +++ b/android-activity/src/native_activity/input.rs @@ -1,11 +1,11 @@ use std::marker::PhantomData; -pub use ndk::event::{ - Axis, ButtonState, EdgeFlags, KeyAction, KeyEventFlags, Keycode, MetaState, MotionAction, - MotionEventFlags, Pointer, PointersIter, -}; +pub use ndk::event::{Pointer, PointersIter}; -use crate::input::{Class, Source}; +use crate::input::{ + ButtonState, Class, EdgeFlags, KeyAction, Keycode, MetaState, MotionAction, MotionEventFlags, + Source, +}; /// A motion event /// @@ -60,7 +60,7 @@ impl<'a> MotionEvent<'a> { /// See [the MotionEvent docs](https://developer.android.com/reference/android/view/MotionEvent#getActionMasked()) #[inline] pub fn action(&self) -> MotionAction { - self.ndk_event.action() + self.ndk_event.action().into() } /// Returns the pointer index of an `Up` or `Down` event. @@ -136,7 +136,7 @@ impl<'a> MotionEvent<'a> { /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getmetastate) #[inline] pub fn meta_state(&self) -> MetaState { - self.ndk_event.meta_state() + self.ndk_event.meta_state().into() } /// Returns the button state during this event, as a bitfield. @@ -145,7 +145,7 @@ impl<'a> MotionEvent<'a> { /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getbuttonstate) #[inline] pub fn button_state(&self) -> ButtonState { - self.ndk_event.button_state() + self.ndk_event.button_state().into() } /// Returns the time of the start of this gesture, in the `java.lang.System.nanoTime()` time @@ -164,7 +164,7 @@ impl<'a> MotionEvent<'a> { /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getedgeflags) #[inline] pub fn edge_flags(&self) -> EdgeFlags { - self.ndk_event.edge_flags() + self.ndk_event.edge_flags().into() } /// Returns the time of this event, in the `java.lang.System.nanoTime()` time base @@ -182,7 +182,7 @@ impl<'a> MotionEvent<'a> { /// docs](https://developer.android.com/ndk/reference/group/input#amotionevent_getflags) #[inline] pub fn flags(&self) -> MotionEventFlags { - self.ndk_event.flags() + self.ndk_event.flags().into() } /* Missing from GameActivity currently... @@ -277,7 +277,7 @@ impl<'a> KeyEvent<'a> { /// See [the KeyEvent docs](https://developer.android.com/reference/android/view/KeyEvent#getAction()) #[inline] pub fn action(&self) -> KeyAction { - self.ndk_event.action() + self.ndk_event.action().into() } /// Returns the last time the key was pressed. This is on the scale of @@ -306,7 +306,7 @@ impl<'a> KeyEvent<'a> { /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getkeycode) #[inline] pub fn key_code(&self) -> Keycode { - self.ndk_event.key_code() + self.ndk_event.key_code().into() } /// Returns the number of repeats of a key. @@ -333,7 +333,7 @@ impl<'a> KeyEvent<'a> { /// docs](https://developer.android.com/ndk/reference/group/input#akeyevent_getmetastate) #[inline] pub fn meta_state(&self) -> MetaState { - self.ndk_event.meta_state() + self.ndk_event.meta_state().into() } } diff --git a/android-activity/src/native_activity/mod.rs b/android-activity/src/native_activity/mod.rs index fee09b9..a18297c 100644 --- a/android-activity/src/native_activity/mod.rs +++ b/android-activity/src/native_activity/mod.rs @@ -14,7 +14,7 @@ use ndk::input_queue::InputQueue; use ndk::{asset::AssetManager, native_window::NativeWindow}; use crate::error::{InternalAppError, InternalResult}; -use crate::input::{KeyCharacterMap, KeyCharacterMapBinding}; +use crate::input::{Axis, KeyCharacterMap, KeyCharacterMapBinding}; use crate::input::{TextInputState, TextSpan}; use crate::jni_utils::{self, CloneJavaVM}; use crate::{ @@ -410,11 +410,11 @@ impl AndroidAppInner { Ok(key_map) } - pub fn enable_motion_axis(&self, _axis: input::Axis) { + pub fn enable_motion_axis(&self, _axis: Axis) { // NOP - The InputQueue API doesn't let us optimize which axis values are read } - pub fn disable_motion_axis(&self, _axis: input::Axis) { + pub fn disable_motion_axis(&self, _axis: Axis) { // NOP - The InputQueue API doesn't let us optimize which axis values are read }