Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Prabir Pradhan | 9244aea | 2020-02-05 20:31:40 -0800 | [diff] [blame] | 17 | #include "../Macros.h" |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 18 | |
| 19 | #include "JoystickInputMapper.h" |
| 20 | |
| 21 | namespace android { |
| 22 | |
Arpit Singh | 8e6fb25 | 2023-04-06 11:49:17 +0000 | [diff] [blame] | 23 | JoystickInputMapper::JoystickInputMapper(InputDeviceContext& deviceContext, |
| 24 | const InputReaderConfiguration& readerConfig) |
| 25 | : InputMapper(deviceContext, readerConfig) {} |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 26 | |
| 27 | JoystickInputMapper::~JoystickInputMapper() {} |
| 28 | |
Philip Junker | 4af3b3d | 2021-12-14 10:36:55 +0100 | [diff] [blame] | 29 | uint32_t JoystickInputMapper::getSources() const { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 30 | return AINPUT_SOURCE_JOYSTICK; |
| 31 | } |
| 32 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 33 | void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo& info) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 34 | InputMapper::populateDeviceInfo(info); |
| 35 | |
Siarhei Vishniakou | 12c0fcb | 2021-12-17 13:40:44 -0800 | [diff] [blame] | 36 | for (const auto& [_, axis] : mAxes) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 37 | addMotionRange(axis.axisInfo.axis, axis, info); |
| 38 | |
| 39 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 40 | addMotionRange(axis.axisInfo.highAxis, axis, info); |
| 41 | } |
| 42 | } |
| 43 | } |
| 44 | |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 45 | void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo& info) { |
| 46 | info.addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK, axis.min, axis.max, axis.flat, axis.fuzz, |
| 47 | axis.resolution); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 48 | /* In order to ease the transition for developers from using the old axes |
| 49 | * to the newer, more semantically correct axes, we'll continue to register |
| 50 | * the old axes as duplicates of their corresponding new ones. */ |
| 51 | int32_t compatAxis = getCompatAxis(axisId); |
| 52 | if (compatAxis >= 0) { |
Harry Cutts | d02ea10 | 2023-03-17 18:21:30 +0000 | [diff] [blame] | 53 | info.addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK, axis.min, axis.max, axis.flat, |
| 54 | axis.fuzz, axis.resolution); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | |
| 58 | /* A mapping from axes the joystick actually has to the axes that should be |
| 59 | * artificially created for compatibility purposes. |
| 60 | * Returns -1 if no compatibility axis is needed. */ |
| 61 | int32_t JoystickInputMapper::getCompatAxis(int32_t axis) { |
| 62 | switch (axis) { |
| 63 | case AMOTION_EVENT_AXIS_LTRIGGER: |
| 64 | return AMOTION_EVENT_AXIS_BRAKE; |
| 65 | case AMOTION_EVENT_AXIS_RTRIGGER: |
| 66 | return AMOTION_EVENT_AXIS_GAS; |
| 67 | } |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | void JoystickInputMapper::dump(std::string& dump) { |
| 72 | dump += INDENT2 "Joystick Input Mapper:\n"; |
| 73 | |
| 74 | dump += INDENT3 "Axes:\n"; |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 75 | for (const auto& [rawAxis, axis] : mAxes) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 76 | const char* label = InputEventLookup::getAxisLabel(axis.axisInfo.axis); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 77 | if (label) { |
| 78 | dump += StringPrintf(INDENT4 "%s", label); |
| 79 | } else { |
| 80 | dump += StringPrintf(INDENT4 "%d", axis.axisInfo.axis); |
| 81 | } |
| 82 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Chris Ye | 4958d06 | 2020-08-20 13:21:10 -0700 | [diff] [blame] | 83 | label = InputEventLookup::getAxisLabel(axis.axisInfo.highAxis); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 84 | if (label) { |
| 85 | dump += StringPrintf(" / %s (split at %d)", label, axis.axisInfo.splitValue); |
| 86 | } else { |
| 87 | dump += StringPrintf(" / %d (split at %d)", axis.axisInfo.highAxis, |
| 88 | axis.axisInfo.splitValue); |
| 89 | } |
| 90 | } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) { |
| 91 | dump += " (invert)"; |
| 92 | } |
| 93 | |
| 94 | dump += StringPrintf(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n", |
| 95 | axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution); |
| 96 | dump += StringPrintf(INDENT4 " scale=%0.5f, offset=%0.5f, " |
| 97 | "highScale=%0.5f, highOffset=%0.5f\n", |
| 98 | axis.scale, axis.offset, axis.highScale, axis.highOffset); |
| 99 | dump += StringPrintf(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " |
| 100 | "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 101 | rawAxis, axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 102 | axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, |
| 103 | axis.rawAxisInfo.resolution); |
| 104 | } |
| 105 | } |
| 106 | |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 107 | std::list<NotifyArgs> JoystickInputMapper::reconfigure(nsecs_t when, |
Arpit Singh | ed6c3de | 2023-04-05 19:24:37 +0000 | [diff] [blame] | 108 | const InputReaderConfiguration& config, |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame^] | 109 | ConfigurationChanges changes) { |
Arpit Singh | 4be4eef | 2023-03-28 14:26:01 +0000 | [diff] [blame] | 110 | std::list<NotifyArgs> out = InputMapper::reconfigure(when, config, changes); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 111 | |
Prabir Pradhan | 4bf6d45 | 2023-04-18 21:26:56 +0000 | [diff] [blame^] | 112 | if (!changes.any()) { // first time only |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 113 | // Collect all axes. |
| 114 | for (int32_t abs = 0; abs <= ABS_MAX; abs++) { |
Chris Ye | 1b0c734 | 2020-07-28 21:57:03 -0700 | [diff] [blame] | 115 | if (!(getAbsAxisUsage(abs, getDeviceContext().getDeviceClasses()) |
| 116 | .test(InputDeviceClass::JOYSTICK))) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 117 | continue; // axis must be claimed by a different device |
| 118 | } |
| 119 | |
| 120 | RawAbsoluteAxisInfo rawAxisInfo; |
| 121 | getAbsoluteAxisInfo(abs, &rawAxisInfo); |
| 122 | if (rawAxisInfo.valid) { |
| 123 | // Map axis. |
| 124 | AxisInfo axisInfo; |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 125 | const bool explicitlyMapped = !getDeviceContext().mapAxis(abs, &axisInfo); |
| 126 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 127 | if (!explicitlyMapped) { |
| 128 | // Axis is not explicitly mapped, will choose a generic axis later. |
| 129 | axisInfo.mode = AxisInfo::MODE_NORMAL; |
| 130 | axisInfo.axis = -1; |
| 131 | } |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 132 | mAxes.insert({abs, createAxis(axisInfo, rawAxisInfo, explicitlyMapped)}); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 133 | } |
| 134 | } |
| 135 | |
| 136 | // If there are too many axes, start dropping them. |
| 137 | // Prefer to keep explicitly mapped axes. |
| 138 | if (mAxes.size() > PointerCoords::MAX_AXES) { |
| 139 | ALOGI("Joystick '%s' has %zu axes but the framework only supports a maximum of %d.", |
| 140 | getDeviceName().c_str(), mAxes.size(), PointerCoords::MAX_AXES); |
| 141 | pruneAxes(true); |
| 142 | pruneAxes(false); |
| 143 | } |
| 144 | |
| 145 | // Assign generic axis ids to remaining axes. |
| 146 | int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 147 | for (auto it = mAxes.begin(); it != mAxes.end(); /*increment it inside loop*/) { |
| 148 | Axis& axis = it->second; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 149 | if (axis.axisInfo.axis < 0) { |
Yeabkal Wubshit | eee671e | 2022-10-06 15:13:34 -0700 | [diff] [blame] | 150 | while (nextGenericAxisId <= AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE && |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 151 | haveAxis(nextGenericAxisId)) { |
| 152 | nextGenericAxisId += 1; |
| 153 | } |
| 154 | |
Yeabkal Wubshit | eee671e | 2022-10-06 15:13:34 -0700 | [diff] [blame] | 155 | if (nextGenericAxisId <= AMOTION_EVENT_MAXIMUM_VALID_AXIS_VALUE) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 156 | axis.axisInfo.axis = nextGenericAxisId; |
| 157 | nextGenericAxisId += 1; |
| 158 | } else { |
| 159 | ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " |
| 160 | "have already been assigned to other axes.", |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 161 | getDeviceName().c_str(), it->first); |
| 162 | it = mAxes.erase(it); |
| 163 | continue; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 164 | } |
| 165 | } |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 166 | it++; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 167 | } |
| 168 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 169 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 172 | JoystickInputMapper::Axis JoystickInputMapper::createAxis(const AxisInfo& axisInfo, |
| 173 | const RawAbsoluteAxisInfo& rawAxisInfo, |
| 174 | bool explicitlyMapped) { |
| 175 | // Apply flat override. |
| 176 | int32_t rawFlat = axisInfo.flatOverride < 0 ? rawAxisInfo.flat : axisInfo.flatOverride; |
| 177 | |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 178 | float scale = std::numeric_limits<float>::signaling_NaN(); |
| 179 | float highScale = std::numeric_limits<float>::signaling_NaN(); |
| 180 | float highOffset = 0; |
| 181 | float offset = 0; |
| 182 | float min = 0; |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 183 | // Calculate scaling factors and limits. |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 184 | if (axisInfo.mode == AxisInfo::MODE_SPLIT) { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 185 | scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); |
| 186 | highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 187 | } else if (isCenteredAxis(axisInfo.axis)) { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 188 | scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 189 | offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; |
| 190 | highOffset = offset; |
| 191 | highScale = scale; |
| 192 | min = -1.0f; |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 193 | } else { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 194 | scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); |
| 195 | highScale = scale; |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 196 | } |
| 197 | |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 198 | constexpr float max = 1.0; |
| 199 | const float flat = rawFlat * scale; |
| 200 | const float fuzz = rawAxisInfo.fuzz * scale; |
| 201 | const float resolution = rawAxisInfo.resolution * scale; |
| 202 | |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 203 | // To eliminate noise while the joystick is at rest, filter out small variations |
| 204 | // in axis values up front. |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 205 | const float filter = fuzz ? fuzz : flat * 0.25f; |
| 206 | return Axis(rawAxisInfo, axisInfo, explicitlyMapped, scale, offset, highScale, highOffset, min, |
| 207 | max, flat, fuzz, resolution, filter); |
Siarhei Vishniakou | 690f8be | 2020-06-30 04:31:25 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 210 | bool JoystickInputMapper::haveAxis(int32_t axisId) { |
Michael Wright | e2f38ac | 2021-01-19 00:59:08 +0000 | [diff] [blame] | 211 | for (const std::pair<const int32_t, Axis>& pair : mAxes) { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 212 | const Axis& axis = pair.second; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 213 | if (axis.axisInfo.axis == axisId || |
| 214 | (axis.axisInfo.mode == AxisInfo::MODE_SPLIT && axis.axisInfo.highAxis == axisId)) { |
| 215 | return true; |
| 216 | } |
| 217 | } |
| 218 | return false; |
| 219 | } |
| 220 | |
| 221 | void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 222 | while (mAxes.size() > PointerCoords::MAX_AXES) { |
| 223 | auto it = mAxes.begin(); |
| 224 | if (ignoreExplicitlyMappedAxes && it->second.explicitlyMapped) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 225 | continue; |
| 226 | } |
| 227 | ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 228 | getDeviceName().c_str(), it->first); |
| 229 | mAxes.erase(it); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
| 233 | bool JoystickInputMapper::isCenteredAxis(int32_t axis) { |
| 234 | switch (axis) { |
| 235 | case AMOTION_EVENT_AXIS_X: |
| 236 | case AMOTION_EVENT_AXIS_Y: |
| 237 | case AMOTION_EVENT_AXIS_Z: |
| 238 | case AMOTION_EVENT_AXIS_RX: |
| 239 | case AMOTION_EVENT_AXIS_RY: |
| 240 | case AMOTION_EVENT_AXIS_RZ: |
| 241 | case AMOTION_EVENT_AXIS_HAT_X: |
| 242 | case AMOTION_EVENT_AXIS_HAT_Y: |
| 243 | case AMOTION_EVENT_AXIS_ORIENTATION: |
| 244 | case AMOTION_EVENT_AXIS_RUDDER: |
| 245 | case AMOTION_EVENT_AXIS_WHEEL: |
| 246 | return true; |
| 247 | default: |
| 248 | return false; |
| 249 | } |
| 250 | } |
| 251 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 252 | std::list<NotifyArgs> JoystickInputMapper::reset(nsecs_t when) { |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 253 | // Recenter all axes. |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 254 | for (std::pair<const int32_t, Axis>& pair : mAxes) { |
| 255 | Axis& axis = pair.second; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 256 | axis.resetValue(); |
| 257 | } |
| 258 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 259 | return InputMapper::reset(when); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 260 | } |
| 261 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 262 | std::list<NotifyArgs> JoystickInputMapper::process(const RawEvent* rawEvent) { |
| 263 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 264 | switch (rawEvent->type) { |
| 265 | case EV_ABS: { |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 266 | auto it = mAxes.find(rawEvent->code); |
| 267 | if (it != mAxes.end()) { |
| 268 | Axis& axis = it->second; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 269 | float newValue, highNewValue; |
| 270 | switch (axis.axisInfo.mode) { |
| 271 | case AxisInfo::MODE_INVERT: |
| 272 | newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) * axis.scale + |
| 273 | axis.offset; |
| 274 | highNewValue = 0.0f; |
| 275 | break; |
| 276 | case AxisInfo::MODE_SPLIT: |
| 277 | if (rawEvent->value < axis.axisInfo.splitValue) { |
| 278 | newValue = (axis.axisInfo.splitValue - rawEvent->value) * axis.scale + |
| 279 | axis.offset; |
| 280 | highNewValue = 0.0f; |
| 281 | } else if (rawEvent->value > axis.axisInfo.splitValue) { |
| 282 | newValue = 0.0f; |
| 283 | highNewValue = |
| 284 | (rawEvent->value - axis.axisInfo.splitValue) * axis.highScale + |
| 285 | axis.highOffset; |
| 286 | } else { |
| 287 | newValue = 0.0f; |
| 288 | highNewValue = 0.0f; |
| 289 | } |
| 290 | break; |
| 291 | default: |
| 292 | newValue = rawEvent->value * axis.scale + axis.offset; |
| 293 | highNewValue = 0.0f; |
| 294 | break; |
| 295 | } |
| 296 | axis.newValue = newValue; |
| 297 | axis.highNewValue = highNewValue; |
| 298 | } |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | case EV_SYN: |
| 303 | switch (rawEvent->code) { |
| 304 | case SYN_REPORT: |
Harry Cutts | 3347623 | 2023-01-30 19:57:29 +0000 | [diff] [blame] | 305 | out += sync(rawEvent->when, rawEvent->readTime, /*force=*/false); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 306 | break; |
| 307 | } |
| 308 | break; |
| 309 | } |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 310 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 311 | } |
| 312 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 313 | std::list<NotifyArgs> JoystickInputMapper::sync(nsecs_t when, nsecs_t readTime, bool force) { |
| 314 | std::list<NotifyArgs> out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 315 | if (!filterAxes(force)) { |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 316 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 317 | } |
| 318 | |
Nathaniel R. Lewis | 26ec222 | 2020-01-10 16:30:54 -0800 | [diff] [blame] | 319 | int32_t metaState = getContext()->getGlobalMetaState(); |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 320 | int32_t buttonState = 0; |
| 321 | |
| 322 | PointerProperties pointerProperties; |
| 323 | pointerProperties.clear(); |
| 324 | pointerProperties.id = 0; |
Siarhei Vishniakou | 6d73f83 | 2022-07-21 17:27:03 -0700 | [diff] [blame] | 325 | pointerProperties.toolType = ToolType::UNKNOWN; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 326 | |
| 327 | PointerCoords pointerCoords; |
| 328 | pointerCoords.clear(); |
| 329 | |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 330 | for (std::pair<const int32_t, Axis>& pair : mAxes) { |
| 331 | const Axis& axis = pair.second; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 332 | setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue); |
| 333 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 334 | setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis, |
| 335 | axis.highCurrentValue); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | // Moving a joystick axis should not wake the device because joysticks can |
| 340 | // be fairly noisy even when not in use. On the other hand, pushing a gamepad |
| 341 | // button will likely wake the device. |
| 342 | // TODO: Use the input device configuration to control this behavior more finely. |
| 343 | uint32_t policyFlags = 0; |
Arthur Hung | 6d5b4b2 | 2022-01-21 07:21:10 +0000 | [diff] [blame] | 344 | int32_t displayId = ADISPLAY_ID_NONE; |
| 345 | if (getDeviceContext().getAssociatedViewport()) { |
| 346 | displayId = getDeviceContext().getAssociatedViewport()->displayId; |
| 347 | } |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 348 | |
Siarhei Vishniakou | 2935db7 | 2022-09-22 13:35:22 -0700 | [diff] [blame] | 349 | out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(), |
| 350 | AINPUT_SOURCE_JOYSTICK, displayId, policyFlags, |
| 351 | AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, |
| 352 | MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, |
| 353 | &pointerProperties, &pointerCoords, 0, 0, |
| 354 | AMOTION_EVENT_INVALID_CURSOR_POSITION, |
| 355 | AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {})); |
| 356 | return out; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis, |
| 360 | float value) { |
| 361 | pointerCoords->setAxisValue(axis, value); |
| 362 | /* In order to ease the transition for developers from using the old axes |
| 363 | * to the newer, more semantically correct axes, we'll continue to produce |
| 364 | * values for the old axes as mirrors of the value of their corresponding |
| 365 | * new axes. */ |
| 366 | int32_t compatAxis = getCompatAxis(axis); |
| 367 | if (compatAxis >= 0) { |
| 368 | pointerCoords->setAxisValue(compatAxis, value); |
| 369 | } |
| 370 | } |
| 371 | |
| 372 | bool JoystickInputMapper::filterAxes(bool force) { |
| 373 | bool atLeastOneSignificantChange = force; |
Siarhei Vishniakou | b4d960d | 2019-10-03 15:38:44 -0500 | [diff] [blame] | 374 | for (std::pair<const int32_t, Axis>& pair : mAxes) { |
| 375 | Axis& axis = pair.second; |
Prabir Pradhan | baa5c82 | 2019-08-30 15:27:05 -0700 | [diff] [blame] | 376 | if (force || |
| 377 | hasValueChangedSignificantly(axis.filter, axis.newValue, axis.currentValue, axis.min, |
| 378 | axis.max)) { |
| 379 | axis.currentValue = axis.newValue; |
| 380 | atLeastOneSignificantChange = true; |
| 381 | } |
| 382 | if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { |
| 383 | if (force || |
| 384 | hasValueChangedSignificantly(axis.filter, axis.highNewValue, axis.highCurrentValue, |
| 385 | axis.min, axis.max)) { |
| 386 | axis.highCurrentValue = axis.highNewValue; |
| 387 | atLeastOneSignificantChange = true; |
| 388 | } |
| 389 | } |
| 390 | } |
| 391 | return atLeastOneSignificantChange; |
| 392 | } |
| 393 | |
| 394 | bool JoystickInputMapper::hasValueChangedSignificantly(float filter, float newValue, |
| 395 | float currentValue, float min, float max) { |
| 396 | if (newValue != currentValue) { |
| 397 | // Filter out small changes in value unless the value is converging on the axis |
| 398 | // bounds or center point. This is intended to reduce the amount of information |
| 399 | // sent to applications by particularly noisy joysticks (such as PS3). |
| 400 | if (fabs(newValue - currentValue) > filter || |
| 401 | hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) || |
| 402 | hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) || |
| 403 | hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) { |
| 404 | return true; |
| 405 | } |
| 406 | } |
| 407 | return false; |
| 408 | } |
| 409 | |
| 410 | bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(float filter, float newValue, |
| 411 | float currentValue, |
| 412 | float thresholdValue) { |
| 413 | float newDistance = fabs(newValue - thresholdValue); |
| 414 | if (newDistance < filter) { |
| 415 | float oldDistance = fabs(currentValue - thresholdValue); |
| 416 | if (newDistance < oldDistance) { |
| 417 | return true; |
| 418 | } |
| 419 | } |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | } // namespace android |