blob: 9725b00212eb0dbb0ade8d6fd9e0fb7de44f15e7 [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
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -070026#[repr(u32)]
27pub enum SourceClass {
28 None = input_bindgen::AINPUT_SOURCE_CLASS_NONE,
29 Button = input_bindgen::AINPUT_SOURCE_CLASS_BUTTON,
30 Pointer = input_bindgen::AINPUT_SOURCE_CLASS_POINTER,
31 Navigation = input_bindgen::AINPUT_SOURCE_CLASS_NAVIGATION,
32 Position = input_bindgen::AINPUT_SOURCE_CLASS_POSITION,
33 Joystick = input_bindgen::AINPUT_SOURCE_CLASS_JOYSTICK,
34}
35
36bitflags! {
37 /// Source of the input device or input events.
38 pub struct Source: u32 {
39 /// SOURCE_UNKNOWN
40 const Unknown = input_bindgen::AINPUT_SOURCE_UNKNOWN;
41 /// SOURCE_KEYBOARD
42 const Keyboard = input_bindgen::AINPUT_SOURCE_KEYBOARD;
43 /// SOURCE_DPAD
44 const Dpad = input_bindgen::AINPUT_SOURCE_DPAD;
45 /// SOURCE_GAMEPAD
46 const Gamepad = input_bindgen::AINPUT_SOURCE_GAMEPAD;
47 /// SOURCE_TOUCHSCREEN
48 const Touchscreen = input_bindgen::AINPUT_SOURCE_TOUCHSCREEN;
49 /// SOURCE_MOUSE
50 const Mouse = input_bindgen::AINPUT_SOURCE_MOUSE;
51 /// SOURCE_STYLUS
52 const Stylus = input_bindgen::AINPUT_SOURCE_STYLUS;
53 /// SOURCE_BLUETOOTH_STYLUS
54 const BluetoothStylus = input_bindgen::AINPUT_SOURCE_BLUETOOTH_STYLUS;
55 /// SOURCE_TRACKBALL
56 const Trackball = input_bindgen::AINPUT_SOURCE_TRACKBALL;
57 /// SOURCE_MOUSE_RELATIVE
58 const MouseRelative = input_bindgen::AINPUT_SOURCE_MOUSE_RELATIVE;
59 /// SOURCE_TOUCHPAD
60 const Touchpad = input_bindgen::AINPUT_SOURCE_TOUCHPAD;
61 /// SOURCE_TOUCH_NAVIGATION
62 const TouchNavigation = input_bindgen::AINPUT_SOURCE_TOUCH_NAVIGATION;
63 /// SOURCE_JOYSTICK
64 const Joystick = input_bindgen::AINPUT_SOURCE_JOYSTICK;
65 /// SOURCE_HDMI
66 const HDMI = input_bindgen::AINPUT_SOURCE_HDMI;
67 /// SOURCE_SENSOR
68 const Sensor = input_bindgen::AINPUT_SOURCE_SENSOR;
69 /// SOURCE_ROTARY_ENCODER
70 const RotaryEncoder = input_bindgen::AINPUT_SOURCE_ROTARY_ENCODER;
71 }
72}
73
Prabir Pradhan0762b1f2023-06-22 23:08:18 +000074/// A rust enum representation of a MotionEvent action.
75#[repr(u32)]
76pub enum MotionAction {
77 /// ACTION_DOWN
78 Down = input_bindgen::AMOTION_EVENT_ACTION_DOWN,
79 /// ACTION_UP
80 Up = input_bindgen::AMOTION_EVENT_ACTION_UP,
81 /// ACTION_MOVE
82 Move = input_bindgen::AMOTION_EVENT_ACTION_MOVE,
83 /// ACTION_CANCEL
84 Cancel = input_bindgen::AMOTION_EVENT_ACTION_CANCEL,
85 /// ACTION_OUTSIDE
86 Outside = input_bindgen::AMOTION_EVENT_ACTION_OUTSIDE,
87 /// ACTION_POINTER_DOWN
88 PointerDown {
89 /// The index of the affected pointer.
90 action_index: usize,
91 } = input_bindgen::AMOTION_EVENT_ACTION_POINTER_DOWN,
92 /// ACTION_POINTER_UP
93 PointerUp {
94 /// The index of the affected pointer.
95 action_index: usize,
96 } = input_bindgen::AMOTION_EVENT_ACTION_POINTER_UP,
97 /// ACTION_HOVER_ENTER
98 HoverEnter = input_bindgen::AMOTION_EVENT_ACTION_HOVER_ENTER,
99 /// ACTION_HOVER_MOVE
100 HoverMove = input_bindgen::AMOTION_EVENT_ACTION_HOVER_MOVE,
101 /// ACTION_HOVER_EXIT
102 HoverExit = input_bindgen::AMOTION_EVENT_ACTION_HOVER_EXIT,
103 /// ACTION_SCROLL
104 Scroll = input_bindgen::AMOTION_EVENT_ACTION_SCROLL,
105 /// ACTION_BUTTON_PRESS
106 ButtonPress = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS,
107 /// ACTION_BUTTON_RELEASE
108 ButtonRelease = input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE,
109}
110
111impl fmt::Display for MotionAction {
112 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
113 match self {
114 MotionAction::Down => write!(f, "DOWN"),
115 MotionAction::Up => write!(f, "UP"),
116 MotionAction::Move => write!(f, "MOVE"),
117 MotionAction::Cancel => write!(f, "CANCEL"),
118 MotionAction::Outside => write!(f, "OUTSIDE"),
119 MotionAction::PointerDown { action_index } => {
120 write!(f, "POINTER_DOWN({})", action_index)
121 }
122 MotionAction::PointerUp { action_index } => write!(f, "POINTER_UP({})", action_index),
123 MotionAction::HoverMove => write!(f, "HOVER_MOVE"),
124 MotionAction::Scroll => write!(f, "SCROLL"),
125 MotionAction::HoverEnter => write!(f, "HOVER_ENTER"),
126 MotionAction::HoverExit => write!(f, "HOVER_EXIT"),
127 MotionAction::ButtonPress => write!(f, "BUTTON_PRESS"),
128 MotionAction::ButtonRelease => write!(f, "BUTTON_RELEASE"),
129 }
130 }
131}
132
133impl From<u32> for MotionAction {
134 fn from(action: u32) -> Self {
135 let (action_masked, action_index) = MotionAction::breakdown_action(action);
136 match action_masked {
137 input_bindgen::AMOTION_EVENT_ACTION_DOWN => MotionAction::Down,
138 input_bindgen::AMOTION_EVENT_ACTION_UP => MotionAction::Up,
139 input_bindgen::AMOTION_EVENT_ACTION_MOVE => MotionAction::Move,
140 input_bindgen::AMOTION_EVENT_ACTION_CANCEL => MotionAction::Cancel,
141 input_bindgen::AMOTION_EVENT_ACTION_OUTSIDE => MotionAction::Outside,
142 input_bindgen::AMOTION_EVENT_ACTION_POINTER_DOWN => {
143 MotionAction::PointerDown { action_index }
144 }
145 input_bindgen::AMOTION_EVENT_ACTION_POINTER_UP => {
146 MotionAction::PointerUp { action_index }
147 }
148 input_bindgen::AMOTION_EVENT_ACTION_HOVER_ENTER => MotionAction::HoverEnter,
149 input_bindgen::AMOTION_EVENT_ACTION_HOVER_MOVE => MotionAction::HoverMove,
150 input_bindgen::AMOTION_EVENT_ACTION_HOVER_EXIT => MotionAction::HoverExit,
151 input_bindgen::AMOTION_EVENT_ACTION_SCROLL => MotionAction::Scroll,
152 input_bindgen::AMOTION_EVENT_ACTION_BUTTON_PRESS => MotionAction::ButtonPress,
153 input_bindgen::AMOTION_EVENT_ACTION_BUTTON_RELEASE => MotionAction::ButtonRelease,
154 _ => panic!("Unknown action: {}", action),
155 }
156 }
157}
158
159impl MotionAction {
160 fn breakdown_action(action: u32) -> (u32, usize) {
161 let action_masked = action & input_bindgen::AMOTION_EVENT_ACTION_MASK;
162 let index = (action & input_bindgen::AMOTION_EVENT_ACTION_POINTER_INDEX_MASK)
163 >> input_bindgen::AMOTION_EVENT_ACTION_POINTER_INDEX_SHIFT;
164 (action_masked, index.try_into().unwrap())
165 }
166}
167
168bitflags! {
169 /// MotionEvent flags.
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -0700170 pub struct MotionFlags: u32 {
Prabir Pradhan0762b1f2023-06-22 23:08:18 +0000171 /// FLAG_CANCELED
Siarhei Vishniakou227a7f82023-07-18 18:30:32 -0700172 const CANCELED = input_bindgen::AMOTION_EVENT_FLAG_CANCELED as u32;
173 /// FLAG_WINDOW_IS_OBSCURED
174 const WINDOW_IS_OBSCURED = input_bindgen::AMOTION_EVENT_FLAG_WINDOW_IS_OBSCURED;
175 /// FLAG_WINDOW_IS_PARTIALLY_OBSCURED
176 const WINDOW_IS_PARTIALLY_OBSCURED =
177 input_bindgen::AMOTION_EVENT_FLAG_WINDOW_IS_PARTIALLY_OBSCURED;
178 /// FLAG_IS_ACCESSIBILITY_EVENT
179 const IS_ACCESSIBILITY_EVENT =
180 input_bindgen::AMOTION_EVENT_FLAG_IS_ACCESSIBILITY_EVENT;
181 /// FLAG_NO_FOCUS_CHANGE
182 const NO_FOCUS_CHANGE = input_bindgen::AMOTION_EVENT_FLAG_NO_FOCUS_CHANGE;
Prabir Pradhan0762b1f2023-06-22 23:08:18 +0000183 }
184}
Siarhei Vishniakou2d151ac2023-09-19 13:30:24 -0700185
186impl Source {
187 /// Return true if this source is from the provided class
188 pub fn is_from_class(&self, source_class: SourceClass) -> bool {
189 let class_bits = source_class as u32;
190 self.bits() & class_bits == class_bits
191 }
192}