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 
getUsageSourcesForMotionArgs(const NotifyMotionArgs & motionArgs)53 std::set<InputDeviceUsageSource> getUsageSourcesForMotionArgs(const NotifyMotionArgs& motionArgs) {
54     LOG_ALWAYS_FATAL_IF(motionArgs.getPointerCount() < 1, "Received motion args without pointers");
55     std::set<InputDeviceUsageSource> sources;
56 
57     for (uint32_t i = 0; i < motionArgs.getPointerCount(); i++) {
58         const auto toolType = motionArgs.pointerProperties[i].toolType;
59         if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE)) {
60             if (toolType == ToolType::MOUSE) {
61                 sources.emplace(InputDeviceUsageSource::MOUSE);
62                 continue;
63             }
64             if (toolType == ToolType::FINGER) {
65                 sources.emplace(InputDeviceUsageSource::TOUCHPAD);
66                 continue;
67             }
68             if (isStylusToolType(toolType)) {
69                 sources.emplace(InputDeviceUsageSource::STYLUS_INDIRECT);
70                 continue;
71             }
72         }
73         if (isFromSource(motionArgs.source, AINPUT_SOURCE_MOUSE_RELATIVE) &&
74             toolType == ToolType::MOUSE) {
75             sources.emplace(InputDeviceUsageSource::MOUSE_CAPTURED);
76             continue;
77         }
78         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHPAD) &&
79             toolType == ToolType::FINGER) {
80             sources.emplace(InputDeviceUsageSource::TOUCHPAD_CAPTURED);
81             continue;
82         }
83         if (isFromSource(motionArgs.source, AINPUT_SOURCE_BLUETOOTH_STYLUS) &&
84             isStylusToolType(toolType)) {
85             sources.emplace(InputDeviceUsageSource::STYLUS_FUSED);
86             continue;
87         }
88         if (isFromSource(motionArgs.source, AINPUT_SOURCE_STYLUS) && isStylusToolType(toolType)) {
89             sources.emplace(InputDeviceUsageSource::STYLUS_DIRECT);
90             continue;
91         }
92         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCH_NAVIGATION)) {
93             sources.emplace(InputDeviceUsageSource::TOUCH_NAVIGATION);
94             continue;
95         }
96         if (isFromSource(motionArgs.source, AINPUT_SOURCE_JOYSTICK)) {
97             sources.emplace(InputDeviceUsageSource::JOYSTICK);
98             continue;
99         }
100         if (isFromSource(motionArgs.source, AINPUT_SOURCE_ROTARY_ENCODER)) {
101             sources.emplace(InputDeviceUsageSource::ROTARY_ENCODER);
102             continue;
103         }
104         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TRACKBALL)) {
105             sources.emplace(InputDeviceUsageSource::TRACKBALL);
106             continue;
107         }
108         if (isFromSource(motionArgs.source, AINPUT_SOURCE_TOUCHSCREEN)) {
109             sources.emplace(InputDeviceUsageSource::TOUCHSCREEN);
110             continue;
111         }
112         sources.emplace(InputDeviceUsageSource::UNKNOWN);
113     }
114 
115     return sources;
116 }
117 
118 } // namespace android
119