| /* | |
| * Copyright 2026 The Android Open Source Project | |
| * | |
| * Licensed under the Apache License, Version 2.0 (the "License"); | |
| * you may not use this file except in compliance with the License. | |
| * You may obtain a copy of the License at | |
| * | |
| * http://www.apache.org/licenses/LICENSE-2.0 | |
| * | |
| * Unless required by applicable law or agreed to in writing, software | |
| * distributed under the License is distributed on an "AS IS" BASIS, | |
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| * See the License for the specific language governing permissions and | |
| * limitations under the License. | |
| */ | |
| #include <input/InputDeviceUsageSource.h> | |
| #include <input/KeyCodeClassifications.h> | |
| namespace android { | |
| InputDeviceUsageSource getUsageSourceForMotion(uint32_t source, ToolType toolType) { | |
| if (isFromSource(source, AINPUT_SOURCE_MOUSE)) { | |
| if (toolType == ToolType::MOUSE) { | |
| return InputDeviceUsageSource::MOUSE; | |
| } | |
| if (toolType == ToolType::FINGER) { | |
| return InputDeviceUsageSource::TOUCHPAD; | |
| } | |
| if (isStylusToolType(toolType)) { | |
| return InputDeviceUsageSource::STYLUS_INDIRECT; | |
| } | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_MOUSE_RELATIVE) && toolType == ToolType::MOUSE) { | |
| return InputDeviceUsageSource::MOUSE_CAPTURED; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_TOUCHPAD) && toolType == ToolType::FINGER) { | |
| return InputDeviceUsageSource::TOUCHPAD_CAPTURED; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_BLUETOOTH_STYLUS) && isStylusToolType(toolType)) { | |
| return InputDeviceUsageSource::STYLUS_FUSED; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_STYLUS) && isStylusToolType(toolType)) { | |
| return InputDeviceUsageSource::STYLUS_DIRECT; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_TOUCH_NAVIGATION)) { | |
| return InputDeviceUsageSource::TOUCH_NAVIGATION; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_JOYSTICK)) { | |
| return InputDeviceUsageSource::JOYSTICK; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_ROTARY_ENCODER)) { | |
| return InputDeviceUsageSource::ROTARY_ENCODER; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_TRACKBALL)) { | |
| return InputDeviceUsageSource::TRACKBALL; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_TOUCHSCREEN)) { | |
| return InputDeviceUsageSource::TOUCHSCREEN; | |
| } | |
| return InputDeviceUsageSource::UNKNOWN; | |
| } | |
| InputDeviceUsageSource getUsageSourceForMotion(const InputMessage::Body::Motion& motion) { | |
| ToolType toolType = ToolType::UNKNOWN; | |
| if (motion.pointerCount > 0) { | |
| toolType = motion.pointers[0].properties.toolType; | |
| } | |
| return getUsageSourceForMotion(motion.source, toolType); | |
| } | |
| InputDeviceUsageSource getUsageSourceForMotion(const MotionEvent& event) { | |
| ToolType toolType = ToolType::UNKNOWN; | |
| if (event.getPointerCount() > 0) { | |
| toolType = event.getToolType(0); | |
| } | |
| return getUsageSourceForMotion(event.getSource(), toolType); | |
| } | |
| InputDeviceUsageSource getUsageSourceForKey(uint32_t source, int32_t keyCode, | |
| int32_t keyboardType) { | |
| if (!isFromSource(source, AINPUT_SOURCE_KEYBOARD)) { | |
| return InputDeviceUsageSource::UNKNOWN; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_DPAD) && | |
| DPAD_ALL_KEYCODES.count(keyCode) != 0) { | |
| return InputDeviceUsageSource::DPAD; | |
| } | |
| if (isFromSource(source, AINPUT_SOURCE_GAMEPAD) && | |
| GAMEPAD_KEYCODES.count(keyCode) != 0) { | |
| return InputDeviceUsageSource::GAMEPAD; | |
| } | |
| if (keyboardType == AINPUT_KEYBOARD_TYPE_ALPHABETIC) { | |
| return InputDeviceUsageSource::KEYBOARD; | |
| } | |
| return InputDeviceUsageSource::BUTTONS; | |
| } | |
| InputDeviceUsageSource getUsageSourceForKey(const KeyEvent& event, int32_t keyboardType) { | |
| return getUsageSourceForKey(event.getSource(), event.getKeyCode(), keyboardType); | |
| } | |
| } // namespace android |