blob: a308c26b2e311112147a9d1532f37c818c7438bd [file] [log] [blame]
Prabir Pradhan0762b1f2023-06-22 23:08:18 +00001/*
2 * Copyright 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! Common definitions of the Android Input Framework in rust.
18
19use bitflags::bitflags;
20use std::fmt;
21
22/// The InputDevice ID.
23#[derive(Debug, Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
24pub struct DeviceId(pub i32);
25
26/// A rust enum representation of a MotionEvent action.
27#[repr(u32)]
28pub enum MotionAction {
29 /// ACTION_DOWN
30 Down = input_bindgen::AMOTION_EVENT_ACTION_DOWN,
31 /// ACTION_UP
32 Up = input_bindgen::AMOTION_EVENT_ACTION_UP,
33 /// ACTION_MOVE
34 Move = input_bindgen::AMOTION_EVENT_ACTION_MOVE,
35 /// ACTION_CANCEL
36 Cancel = input_bindgen::AMOTION_EVENT_ACTION_CANCEL,
37 /// ACTION_OUTSIDE
38 Outside = input_bindgen::AMOTION_EVENT_ACTION_OUTSIDE,
39 /// ACTION_POINTER_DOWN
40 PointerDown {
41 /// The index of the affected pointer.
42 action_index: usize,
43 } = input_bindgen::AMOTION_EVENT_ACTION_POINTER_DOWN,
44 /// ACTION_POINTER_UP
45 PointerUp {
46 /// The index of the affected pointer.
47 action_index: usize,
48 } = input_bindgen::AMOTION_EVENT_ACTION_POINTER_UP,
49 /// ACTION_HOVER_ENTER
50 HoverEnter = input_bindgen::AMOTION_EVENT_ACTION_HOVER_ENTER,
51 /// ACTION_HOVER_MOVE
52 HoverMove = input_bindgen::AMOTION_EVENT_ACTION_HOVER_MOVE,
53 /// ACTION_HOVER_EXIT
54 HoverExit = input_bindgen::AMOTION_EVENT_ACTION_HOVER_EXIT,
55 /// ACTION_SCROLL
56 Scroll = input_bindgen::AMOTION_EVENT_ACTION_SCROLL,
57 /// ACTION_BUTTON_PRESS
58 ButtonPress = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS,
59 /// ACTION_BUTTON_RELEASE
60 ButtonRelease = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE,
61}
62
63impl fmt::Display for MotionAction {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 match self {
66 MotionAction::Down => write!(f, "DOWN"),
67 MotionAction::Up => write!(f, "UP"),
68 MotionAction::Move => write!(f, "MOVE"),
69 MotionAction::Cancel => write!(f, "CANCEL"),
70 MotionAction::Outside => write!(f, "OUTSIDE"),
71 MotionAction::PointerDown { action_index } => {
72 write!(f, "POINTER_DOWN({})", action_index)
73 }
74 MotionAction::PointerUp { action_index } => write!(f, "POINTER_UP({})", action_index),
75 MotionAction::HoverMove => write!(f, "HOVER_MOVE"),
76 MotionAction::Scroll => write!(f, "SCROLL"),
77 MotionAction::HoverEnter => write!(f, "HOVER_ENTER"),
78 MotionAction::HoverExit => write!(f, "HOVER_EXIT"),
79 MotionAction::ButtonPress => write!(f, "BUTTON_PRESS"),
80 MotionAction::ButtonRelease => write!(f, "BUTTON_RELEASE"),
81 }
82 }
83}
84
85impl From<u32> for MotionAction {
86 fn from(action: u32) -> Self {
87 let (action_masked, action_index) = MotionAction::breakdown_action(action);
88 match action_masked {
89 input_bindgen::AMOTION_EVENT_ACTION_DOWN => MotionAction::Down,
90 input_bindgen::AMOTION_EVENT_ACTION_UP => MotionAction::Up,
91 input_bindgen::AMOTION_EVENT_ACTION_MOVE => MotionAction::Move,
92 input_bindgen::AMOTION_EVENT_ACTION_CANCEL => MotionAction::Cancel,
93 input_bindgen::AMOTION_EVENT_ACTION_OUTSIDE => MotionAction::Outside,
94 input_bindgen::AMOTION_EVENT_ACTION_POINTER_DOWN => {
95 MotionAction::PointerDown { action_index }
96 }
97 input_bindgen::AMOTION_EVENT_ACTION_POINTER_UP => {
98 MotionAction::PointerUp { action_index }
99 }
100 input_bindgen::AMOTION_EVENT_ACTION_HOVER_ENTER => MotionAction::HoverEnter,
101 input_bindgen::AMOTION_EVENT_ACTION_HOVER_MOVE => MotionAction::HoverMove,
102 input_bindgen::AMOTION_EVENT_ACTION_HOVER_EXIT => MotionAction::HoverExit,
103 input_bindgen::AMOTION_EVENT_ACTION_SCROLL => MotionAction::Scroll,
104 input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS => MotionAction::ButtonPress,
105 input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE => MotionAction::ButtonRelease,
106 _ => panic!("Unknown action: {}", action),
107 }
108 }
109}
110
111impl MotionAction {
112 fn breakdown_action(action: u32) -> (u32, usize) {
113 let action_masked = action & input_bindgen::AMOTION_EVENT_ACTION_MASK;
114 let index = (action & input_bindgen::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
115 >> input_bindgen::AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
116 (action_masked, index.try_into().unwrap())
117 }
118}
119
120bitflags! {
121 /// MotionEvent flags.
122 pub struct MotionFlags: i32 {
123 /// FLAG_CANCELED
124 const CANCELED = input_bindgen::AMOTION_EVENT_FLAG_CANCELED;
125 }
126}