Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a2f1f28e authored by Harry Cutts's avatar Harry Cutts Committed by Android (Google) Code Review
Browse files

Merge changes Ida17429b,I3debc83d,If68332d6 into main

* changes:
  InputVerifier: make action_button a field of the action enum
  InputVerifier: use `let ... else` when converting flags and buttons
  InputVerifier: put parameters into a struct
parents c463aa04 34594c3c
Loading
Loading
Loading
Loading
+27 −13
Original line number Diff line number Diff line
@@ -50,7 +50,7 @@ pub enum SourceClass {

bitflags! {
    /// Source of the input device or input events.
    #[derive(Debug, PartialEq)]
    #[derive(Clone, Copy, Debug, PartialEq)]
    pub struct Source: u32 {
        // Constants from SourceClass, added here for compatibility reasons
        /// SourceClass::Button
@@ -101,7 +101,7 @@ bitflags! {

/// A rust enum representation of a MotionEvent action.
#[repr(u32)]
#[derive(Eq, PartialEq)]
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum MotionAction {
    /// ACTION_DOWN
    Down = input_bindgen::AMOTION_EVENT_ACTION_DOWN,
@@ -132,9 +132,15 @@ pub enum MotionAction {
    /// ACTION_SCROLL
    Scroll = input_bindgen::AMOTION_EVENT_ACTION_SCROLL,
    /// ACTION_BUTTON_PRESS
    ButtonPress = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS,
    ButtonPress {
        /// The button being pressed.
        action_button: MotionButton,
    } = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS,
    /// ACTION_BUTTON_RELEASE
    ButtonRelease = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE,
    ButtonRelease {
        /// The button being released.
        action_button: MotionButton,
    } = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE,
}

impl fmt::Display for MotionAction {
@@ -153,14 +159,20 @@ impl fmt::Display for MotionAction {
            MotionAction::Scroll => write!(f, "SCROLL"),
            MotionAction::HoverEnter => write!(f, "HOVER_ENTER"),
            MotionAction::HoverExit => write!(f, "HOVER_EXIT"),
            MotionAction::ButtonPress => write!(f, "BUTTON_PRESS"),
            MotionAction::ButtonRelease => write!(f, "BUTTON_RELEASE"),
            MotionAction::ButtonPress { action_button } => {
                write!(f, "BUTTON_PRESS({action_button:?})")
            }
            MotionAction::ButtonRelease { action_button } => {
                write!(f, "BUTTON_RELEASE({action_button:?})")
            }
        }
    }
}

impl From<u32> for MotionAction {
    fn from(action: u32) -> Self {
impl MotionAction {
    /// Creates a [`MotionAction`] from an `AMOTION_EVENT_ACTION_…` constant and an action button
    /// (which should be empty for all actions except `BUTTON_PRESS` and `…_RELEASE`).
    pub fn from_code(action: u32, action_button: MotionButton) -> Self {
        let (action_masked, action_index) = MotionAction::breakdown_action(action);
        match action_masked {
            input_bindgen::AMOTION_EVENT_ACTION_DOWN => MotionAction::Down,
@@ -178,14 +190,16 @@ impl From<u32> for MotionAction {
            input_bindgen::AMOTION_EVENT_ACTION_HOVER_MOVE => MotionAction::HoverMove,
            input_bindgen::AMOTION_EVENT_ACTION_HOVER_EXIT => MotionAction::HoverExit,
            input_bindgen::AMOTION_EVENT_ACTION_SCROLL => MotionAction::Scroll,
            input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS => MotionAction::ButtonPress,
            input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE => MotionAction::ButtonRelease,
            _ => panic!("Unknown action: {}", action),
            input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS => {
                MotionAction::ButtonPress { action_button }
            }
            input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE => {
                MotionAction::ButtonRelease { action_button }
            }
            _ => panic!("Unknown action: {}", action),
        }
    }

impl MotionAction {
    fn breakdown_action(action: u32) -> (u32, usize) {
        let action_masked = action & input_bindgen::AMOTION_EVENT_ACTION_MASK;
        let index = (action & input_bindgen::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
@@ -219,7 +233,7 @@ bitflags! {
    /// MotionEvent flags.
    /// The source of truth for the flag definitions are the MotionEventFlag AIDL enum.
    /// The flag values are redefined here as a bitflags API.
    #[derive(Debug)]
    #[derive(Clone, Copy, Debug)]
    pub struct MotionFlags: u32 {
        /// FLAG_WINDOW_IS_OBSCURED
        const WINDOW_IS_OBSCURED = MotionEventFlag::WINDOW_IS_OBSCURED.0 as u32;
+401 −714

File changed.

Preview size limit exceeded, changes collapsed.

+67 −17
Original line number Diff line number Diff line
@@ -27,7 +27,7 @@ pub use input::{
    DeviceClass, DeviceId, InputDevice, KeyboardType, ModifierState, MotionAction, MotionButton,
    MotionFlags, Source,
};
pub use input_verifier::InputVerifier;
pub use input_verifier::{InputVerifier, NotifyMotionArgs};
pub use keyboard_classifier::KeyboardClassifier;

#[cxx::bridge(namespace = "android::input")]
@@ -133,37 +133,46 @@ fn process_movement(
    flags: u32,
    button_state: u32,
) -> String {
    let motion_flags = MotionFlags::from_bits(flags);
    if motion_flags.is_none() {
    let Some(motion_flags) = MotionFlags::from_bits(flags) else {
        panic!(
            "The conversion of flags 0x{:08x} failed, please check if some flags have not been \
            added to MotionFlags.",
            flags
        );
    }
    let motion_action_button = MotionButton::from_bits(action_button);
    if motion_action_button.is_none() {
    };
    let Some(motion_action_button) = MotionButton::from_bits(action_button) else {
        panic!(
            "The conversion of action button 0x{action_button:08x} failed, please check if some \
             buttons need to be added to MotionButton."
        );
    }
    let motion_button_state = MotionButton::from_bits(button_state);
    if motion_button_state.is_none() {
    };
    let Some(motion_button_state) = MotionButton::from_bits(button_state) else {
        panic!(
            "The conversion of button state 0x{button_state:08x} failed, please check if some \
             buttons need to be added to MotionButton."
        );
    };
    let motion_action = MotionAction::from_code(action, motion_action_button);
    if motion_action_button != MotionButton::empty() {
        match motion_action {
            MotionAction::ButtonPress { action_button: _ }
            | MotionAction::ButtonRelease { action_button: _ } => {}
            _ => {
                return format!(
                    "Invalid {motion_action} event: has action button {motion_action_button:?} but \
                     is not a button action"
                );
            }
        }
    let result = verifier.process_movement(
        DeviceId(device_id),
        Source::from_bits(source).unwrap(),
        action,
        motion_action_button.unwrap(),
    }
    let result = verifier.process_movement(NotifyMotionArgs {
        device_id: DeviceId(device_id),
        source: Source::from_bits(source).unwrap(),
        action: motion_action,
        pointer_properties,
        motion_flags.unwrap(),
        motion_button_state.unwrap(),
    );
        flags: motion_flags,
        button_state: motion_button_state,
    });
    match result {
        Ok(()) => "".to_string(),
        Err(e) => e,
@@ -230,3 +239,44 @@ fn process_key(
    }
    classifier.process_key(DeviceId(device_id), evdev_code, modifier_state.unwrap());
}

#[cfg(test)]
mod tests {
    use crate::create_input_verifier;
    use crate::process_movement;
    use crate::RustPointerProperties;

    const BASE_POINTER_PROPERTIES: [RustPointerProperties; 1] = [RustPointerProperties { id: 0 }];

    #[test]
    fn verify_nonbutton_action_with_action_button() {
        let mut verifier = create_input_verifier("Test".to_string(), /*verify_buttons*/ true);
        assert!(process_movement(
            &mut verifier,
            1,
            input_bindgen::AINPUT_SOURCE_MOUSE,
            input_bindgen::AMOTION_EVENT_ACTION_HOVER_ENTER,
            input_bindgen::AMOTION_EVENT_BUTTON_PRIMARY,
            &BASE_POINTER_PROPERTIES,
            0,
            0,
        )
        .contains("button action"));
    }

    #[test]
    fn verify_nonbutton_action_with_action_button_and_button_state() {
        let mut verifier = create_input_verifier("Test".to_string(), /*verify_buttons*/ true);
        assert!(process_movement(
            &mut verifier,
            1,
            input_bindgen::AINPUT_SOURCE_MOUSE,
            input_bindgen::AMOTION_EVENT_ACTION_HOVER_ENTER,
            input_bindgen::AMOTION_EVENT_BUTTON_PRIMARY,
            &BASE_POINTER_PROPERTIES,
            0,
            input_bindgen::AMOTION_EVENT_BUTTON_PRIMARY,
        )
        .contains("button action"));
    }
}