Skip to content

Commit 34594c3

Browse files
committed
InputVerifier: make action_button a field of the action enum
Only MotionEvents with a button press or release action should have an action button. Let's express this in the data structure by making action_button a field of the relevant MotionActions, instead of a separate field of MotionEvent that could potentially be set on non-button actions. This involves moving the translation from input_bindgen constants to MotionAction into lib.rs, which I think makes more sense (as it keeps the language interfacing code contained there), but also means we have to move one piece of validation there too. Bug: 245989146 Test: $ atest --host libinput_rust_test Test: enable the verifier, check everything works as usual Flag: EXEMPT refactor Change-Id: Ida17429b0e12247b63a3ae44bab63e421d9fff0f
1 parent c932b07 commit 34594c3

3 files changed

Lines changed: 184 additions & 211 deletions

File tree

libs/input/rust/input.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ bitflags! {
101101

102102
/// A rust enum representation of a MotionEvent action.
103103
#[repr(u32)]
104-
#[derive(Eq, PartialEq)]
104+
#[derive(Clone, Copy, Eq, PartialEq)]
105105
pub enum MotionAction {
106106
/// ACTION_DOWN
107107
Down = input_bindgen::AMOTION_EVENT_ACTION_DOWN,
@@ -132,9 +132,15 @@ pub enum MotionAction {
132132
/// ACTION_SCROLL
133133
Scroll = input_bindgen::AMOTION_EVENT_ACTION_SCROLL,
134134
/// ACTION_BUTTON_PRESS
135-
ButtonPress = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS,
135+
ButtonPress {
136+
/// The button being pressed.
137+
action_button: MotionButton,
138+
} = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS,
136139
/// ACTION_BUTTON_RELEASE
137-
ButtonRelease = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE,
140+
ButtonRelease {
141+
/// The button being released.
142+
action_button: MotionButton,
143+
} = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE,
138144
}
139145

140146
impl fmt::Display for MotionAction {
@@ -153,14 +159,20 @@ impl fmt::Display for MotionAction {
153159
MotionAction::Scroll => write!(f, "SCROLL"),
154160
MotionAction::HoverEnter => write!(f, "HOVER_ENTER"),
155161
MotionAction::HoverExit => write!(f, "HOVER_EXIT"),
156-
MotionAction::ButtonPress => write!(f, "BUTTON_PRESS"),
157-
MotionAction::ButtonRelease => write!(f, "BUTTON_RELEASE"),
162+
MotionAction::ButtonPress { action_button } => {
163+
write!(f, "BUTTON_PRESS({action_button:?})")
164+
}
165+
MotionAction::ButtonRelease { action_button } => {
166+
write!(f, "BUTTON_RELEASE({action_button:?})")
167+
}
158168
}
159169
}
160170
}
161171

162-
impl From<u32> for MotionAction {
163-
fn from(action: u32) -> Self {
172+
impl MotionAction {
173+
/// Creates a [`MotionAction`] from an `AMOTION_EVENT_ACTION_…` constant and an action button
174+
/// (which should be empty for all actions except `BUTTON_PRESS` and `…_RELEASE`).
175+
pub fn from_code(action: u32, action_button: MotionButton) -> Self {
164176
let (action_masked, action_index) = MotionAction::breakdown_action(action);
165177
match action_masked {
166178
input_bindgen::AMOTION_EVENT_ACTION_DOWN => MotionAction::Down,
@@ -178,14 +190,16 @@ impl From<u32> for MotionAction {
178190
input_bindgen::AMOTION_EVENT_ACTION_HOVER_MOVE => MotionAction::HoverMove,
179191
input_bindgen::AMOTION_EVENT_ACTION_HOVER_EXIT => MotionAction::HoverExit,
180192
input_bindgen::AMOTION_EVENT_ACTION_SCROLL => MotionAction::Scroll,
181-
input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS => MotionAction::ButtonPress,
182-
input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE => MotionAction::ButtonRelease,
193+
input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS => {
194+
MotionAction::ButtonPress { action_button }
195+
}
196+
input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE => {
197+
MotionAction::ButtonRelease { action_button }
198+
}
183199
_ => panic!("Unknown action: {}", action),
184200
}
185201
}
186-
}
187202

188-
impl MotionAction {
189203
fn breakdown_action(action: u32) -> (u32, usize) {
190204
let action_masked = action & input_bindgen::AMOTION_EVENT_ACTION_MASK;
191205
let index = (action & input_bindgen::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)

0 commit comments

Comments
 (0)