xref: /aosp_15_r20/frameworks/native/services/inputflinger/InputDeviceMetricsSource.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
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 #include "InputDeviceMetricsSource.h"
18 
19 #include "KeyCodeClassifications.h"
20 
21 #include <android/input.h>
22 #include <input/Input.h>
23 #include <linux/input.h>
24 #include <log/log_main.h>
25 
26 #include <set>
27 
28 namespace android {
29 
getUsageSourceForKeyArgs(int32_t keyboardType,const NotifyKeyArgs & keyArgs)30 InputDeviceUsageSource getUsageSourceForKeyArgs(int32_t keyboardType,
31                                                 const NotifyKeyArgs& keyArgs) {
32     if (!isFromSource(keyArgs.source, AINPUT_SOURCE_KEYBOARD)) {
33         return InputDeviceUsageSource::UNKNOWN;
34     }
35 
36     if (isFromSource(keyArgs.source, AINPUT_SOURCE_DPAD) &&
37         DPAD_ALL_KEYCODES.count(keyArgs.keyCode) != 0) {
38         return InputDeviceUsageSource::DPAD;
39     }
40 
41     if (isFromSource(keyArgs.source, AINPUT_SOURCE_GAMEPAD) &&
42         GAMEPAD_KEYCODES.count(keyArgs.keyCode) != 0) {
43         return InputDeviceUsageSource::GAMEPAD;
44     }
45 
46     if (keyboardType == AINPUT_KEYBOARD_TYPE_ALPHABETIC) {
47         return InputDeviceUsageSource::KEYBOARD;
48     }
49 
50     return InputDeviceUsageSource::BUTTONS;
51 }
52 
getUsageSourcesForKeyArgs(const NotifyKeyArgs & args,const std::vector<InputDeviceInfo> & inputDevices)53 std::set<InputDeviceUsageSource> getUsageSourcesForKeyArgs(
54         const NotifyKeyArgs& args, const std::vector<InputDeviceInfo>& inputDevices) {
55     int32_t keyboardType = AINPUT_KEYBOARD_TYPE_NONE;
56     for (const InputDeviceInfo& inputDevice : inputDevices) {
57         if (args.deviceId == inputDevice.getId()) {
58             keyboardType = inputDevice.getKeyboardType();
59             break;
60         }
61     }
62     return std::set{getUsageSourceForKeyArgs(keyboardType, args)};
63 }
64 
getUsageSourcesForMotionArgs(const NotifyMotionArgs & motionArgs)65 std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs& motionArgs) {
66     LOG_ALWAYS_FATAL_IF(motionArgs.getPointerCount() < 1, "Received motion args without pointers");
67     std::set<InputDeviceUsageSource> sources;
68 
69     for (uint32_t i = 0; i < motionArgs.getPointerCount(); i++) {
70         const auto toolType = motionArgs.pointerProperties[i].toolType;
71         if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE)) {
72             if (toolType == ToolType::MOUSE) {
73                 sources.emplace(InputDeviceUsageSource::MOUSE);
74                 continue;
75             }
76             if (toolType == ToolType::FINGER) {
77                 sources.emplace(InputDeviceUsageSource::TOUCHPAD);
78                 continue;
79             }
80             if (isStylusToolType(toolType)) {
81                 sources.emplace(InputDeviceUsageSource::STYLUS_INDIRECT);
82                 continue;
83             }
84         }
85         if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE_RELATIVE) &&
86             toolType == ToolType::MOUSE) {
87             sources.emplace(InputDeviceUsageSource::MOUSE_CAPTURED);
88             continue;
89         }
90         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHPAD) &&
91             toolType == ToolType::FINGER) {
92             sources.emplace(InputDeviceUsageSource::TOUCHPAD_CAPTURED);
93             continue;
94         }
95         if (isFromSource(motionArgs.source, AINPUT_SOURCE_BLUETOOTH_STYLUS) &&
96             isStylusToolType(toolType)) {
97             sources.emplace(InputDeviceUsageSource::STYLUS_FUSED);
98             continue;
99         }
100         if (isFromSource(motionArgs.source, AINPUT_SOURCE_STYLUS) && isStylusToolType(toolType)) {
101             sources.emplace(InputDeviceUsageSource::STYLUS_DIRECT);
102             continue;
103         }
104         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCH_NAVIGATION)) {
105             sources.emplace(InputDeviceUsageSource::TOUCH_NAVIGATION);
106             continue;
107         }
108         if (isFromSource(motionArgs.source, AINPUT_SOURCE_JOYSTICK)) {
109             sources.emplace(InputDeviceUsageSource::JOYSTICK);
110             continue;
111         }
112         if (isFromSource(motionArgs.source, AINPUT_SOURCE_ROTARY_ENCODER)) {
113             sources.emplace(InputDeviceUsageSource::ROTARY_ENCODER);
114             continue;
115         }
116         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TRACKBALL)) {
117             sources.emplace(InputDeviceUsageSource::TRACKBALL);
118             continue;
119         }
120         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHSCREEN)) {
121             sources.emplace(InputDeviceUsageSource::TOUCHSCREEN);
122             continue;
123         }
124         sources.emplace(InputDeviceUsageSource::UNKNOWN);
125     }
126 
127     return sources;
128 }
129 
130 } // namespace android
131