blob: 42b80129a1cd2f503a1dd4031c952d8ef102f7e7 [file] [log] [blame]
Prabir Pradhanbaa5c822019-08-30 15:27:05 -07001/*
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 Pradhan9244aea2020-02-05 20:31:40 -080017#include "../Macros.h"
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070018
19#include "JoystickInputMapper.h"
20
21namespace android {
22
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -080023JoystickInputMapper::JoystickInputMapper(InputDeviceContext& deviceContext)
24 : InputMapper(deviceContext) {}
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070025
26JoystickInputMapper::~JoystickInputMapper() {}
27
Philip Junker4af3b3d2021-12-14 10:36:55 +010028uint32_t JoystickInputMapper::getSources() const {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070029 return AINPUT_SOURCE_JOYSTICK;
30}
31
32void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
33 InputMapper::populateDeviceInfo(info);
34
Siarhei Vishniakou12c0fcb2021-12-17 13:40:44 -080035 for (const auto& [_, axis] : mAxes) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070036 addMotionRange(axis.axisInfo.axis, axis, info);
37
38 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
39 addMotionRange(axis.axisInfo.highAxis, axis, info);
40 }
41 }
42}
43
44void JoystickInputMapper::addMotionRange(int32_t axisId, const Axis& axis, InputDeviceInfo* info) {
45 info->addMotionRange(axisId, AINPUT_SOURCE_JOYSTICK, axis.min, axis.max, axis.flat, axis.fuzz,
46 axis.resolution);
47 /* In order to ease the transition for developers from using the old axes
48 * to the newer, more semantically correct axes, we'll continue to register
49 * the old axes as duplicates of their corresponding new ones. */
50 int32_t compatAxis = getCompatAxis(axisId);
51 if (compatAxis >= 0) {
52 info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK, axis.min, axis.max, axis.flat,
53 axis.fuzz, axis.resolution);
54 }
55}
56
57/* A mapping from axes the joystick actually has to the axes that should be
58 * artificially created for compatibility purposes.
59 * Returns -1 if no compatibility axis is needed. */
60int32_t JoystickInputMapper::getCompatAxis(int32_t axis) {
61 switch (axis) {
62 case AMOTION_EVENT_AXIS_LTRIGGER:
63 return AMOTION_EVENT_AXIS_BRAKE;
64 case AMOTION_EVENT_AXIS_RTRIGGER:
65 return AMOTION_EVENT_AXIS_GAS;
66 }
67 return -1;
68}
69
70void JoystickInputMapper::dump(std::string& dump) {
71 dump += INDENT2 "Joystick Input Mapper:\n";
72
73 dump += INDENT3 "Axes:\n";
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -050074 for (const auto& [rawAxis, axis] : mAxes) {
Chris Ye4958d062020-08-20 13:21:10 -070075 const char* label = InputEventLookup::getAxisLabel(axis.axisInfo.axis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070076 if (label) {
77 dump += StringPrintf(INDENT4 "%s", label);
78 } else {
79 dump += StringPrintf(INDENT4 "%d", axis.axisInfo.axis);
80 }
81 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
Chris Ye4958d062020-08-20 13:21:10 -070082 label = InputEventLookup::getAxisLabel(axis.axisInfo.highAxis);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -070083 if (label) {
84 dump += StringPrintf(" / %s (split at %d)", label, axis.axisInfo.splitValue);
85 } else {
86 dump += StringPrintf(" / %d (split at %d)", axis.axisInfo.highAxis,
87 axis.axisInfo.splitValue);
88 }
89 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
90 dump += " (invert)";
91 }
92
93 dump += StringPrintf(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n",
94 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
95 dump += StringPrintf(INDENT4 " scale=%0.5f, offset=%0.5f, "
96 "highScale=%0.5f, highOffset=%0.5f\n",
97 axis.scale, axis.offset, axis.highScale, axis.highOffset);
98 dump += StringPrintf(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
99 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500100 rawAxis, axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700101 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz,
102 axis.rawAxisInfo.resolution);
103 }
104}
105
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700106std::list<NotifyArgs> JoystickInputMapper::configure(nsecs_t when,
107 const InputReaderConfiguration* config,
108 uint32_t changes) {
109 std::list<NotifyArgs> out = InputMapper::configure(when, config, changes);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700110
111 if (!changes) { // first time only
112 // Collect all axes.
113 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Chris Ye1b0c7342020-07-28 21:57:03 -0700114 if (!(getAbsAxisUsage(abs, getDeviceContext().getDeviceClasses())
115 .test(InputDeviceClass::JOYSTICK))) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700116 continue; // axis must be claimed by a different device
117 }
118
119 RawAbsoluteAxisInfo rawAxisInfo;
120 getAbsoluteAxisInfo(abs, &rawAxisInfo);
121 if (rawAxisInfo.valid) {
122 // Map axis.
123 AxisInfo axisInfo;
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500124 const bool explicitlyMapped = !getDeviceContext().mapAxis(abs, &axisInfo);
125
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700126 if (!explicitlyMapped) {
127 // Axis is not explicitly mapped, will choose a generic axis later.
128 axisInfo.mode = AxisInfo::MODE_NORMAL;
129 axisInfo.axis = -1;
130 }
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500131 mAxes.insert({abs, createAxis(axisInfo, rawAxisInfo, explicitlyMapped)});
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700132 }
133 }
134
135 // If there are too many axes, start dropping them.
136 // Prefer to keep explicitly mapped axes.
137 if (mAxes.size() > PointerCoords::MAX_AXES) {
138 ALOGI("Joystick '%s' has %zu axes but the framework only supports a maximum of %d.",
139 getDeviceName().c_str(), mAxes.size(), PointerCoords::MAX_AXES);
140 pruneAxes(true);
141 pruneAxes(false);
142 }
143
144 // Assign generic axis ids to remaining axes.
145 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500146 for (auto it = mAxes.begin(); it != mAxes.end(); /*increment it inside loop*/) {
147 Axis& axis = it->second;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700148 if (axis.axisInfo.axis < 0) {
149 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 &&
150 haveAxis(nextGenericAxisId)) {
151 nextGenericAxisId += 1;
152 }
153
154 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
155 axis.axisInfo.axis = nextGenericAxisId;
156 nextGenericAxisId += 1;
157 } else {
158 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
159 "have already been assigned to other axes.",
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500160 getDeviceName().c_str(), it->first);
161 it = mAxes.erase(it);
162 continue;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700163 }
164 }
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500165 it++;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700166 }
167 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700168 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700169}
170
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500171JoystickInputMapper::Axis JoystickInputMapper::createAxis(const AxisInfo& axisInfo,
172 const RawAbsoluteAxisInfo& rawAxisInfo,
173 bool explicitlyMapped) {
174 // Apply flat override.
175 int32_t rawFlat = axisInfo.flatOverride < 0 ? rawAxisInfo.flat : axisInfo.flatOverride;
176
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500177 float scale = std::numeric_limits<float>::signaling_NaN();
178 float highScale = std::numeric_limits<float>::signaling_NaN();
179 float highOffset = 0;
180 float offset = 0;
181 float min = 0;
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500182 // Calculate scaling factors and limits.
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500183 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500184 scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
185 highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500186 } else if (isCenteredAxis(axisInfo.axis)) {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500187 scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
188 offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
189 highOffset = offset;
190 highScale = scale;
191 min = -1.0f;
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500192 } else {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500193 scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
194 highScale = scale;
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500195 }
196
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500197 constexpr float max = 1.0;
198 const float flat = rawFlat * scale;
199 const float fuzz = rawAxisInfo.fuzz * scale;
200 const float resolution = rawAxisInfo.resolution * scale;
201
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500202 // To eliminate noise while the joystick is at rest, filter out small variations
203 // in axis values up front.
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500204 const float filter = fuzz ? fuzz : flat * 0.25f;
205 return Axis(rawAxisInfo, axisInfo, explicitlyMapped, scale, offset, highScale, highOffset, min,
206 max, flat, fuzz, resolution, filter);
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500207}
208
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700209bool JoystickInputMapper::haveAxis(int32_t axisId) {
Michael Wrighte2f38ac2021-01-19 00:59:08 +0000210 for (const std::pair<const int32_t, Axis>& pair : mAxes) {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500211 const Axis& axis = pair.second;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700212 if (axis.axisInfo.axis == axisId ||
213 (axis.axisInfo.mode == AxisInfo::MODE_SPLIT && axis.axisInfo.highAxis == axisId)) {
214 return true;
215 }
216 }
217 return false;
218}
219
220void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500221 while (mAxes.size() > PointerCoords::MAX_AXES) {
222 auto it = mAxes.begin();
223 if (ignoreExplicitlyMappedAxes && it->second.explicitlyMapped) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700224 continue;
225 }
226 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500227 getDeviceName().c_str(), it->first);
228 mAxes.erase(it);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700229 }
230}
231
232bool JoystickInputMapper::isCenteredAxis(int32_t axis) {
233 switch (axis) {
234 case AMOTION_EVENT_AXIS_X:
235 case AMOTION_EVENT_AXIS_Y:
236 case AMOTION_EVENT_AXIS_Z:
237 case AMOTION_EVENT_AXIS_RX:
238 case AMOTION_EVENT_AXIS_RY:
239 case AMOTION_EVENT_AXIS_RZ:
240 case AMOTION_EVENT_AXIS_HAT_X:
241 case AMOTION_EVENT_AXIS_HAT_Y:
242 case AMOTION_EVENT_AXIS_ORIENTATION:
243 case AMOTION_EVENT_AXIS_RUDDER:
244 case AMOTION_EVENT_AXIS_WHEEL:
245 return true;
246 default:
247 return false;
248 }
249}
250
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700251std::list<NotifyArgs> JoystickInputMapper::reset(nsecs_t when) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700252 // Recenter all axes.
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500253 for (std::pair<const int32_t, Axis>& pair : mAxes) {
254 Axis& axis = pair.second;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700255 axis.resetValue();
256 }
257
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700258 return InputMapper::reset(when);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700259}
260
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700261std::list<NotifyArgs> JoystickInputMapper::process(const RawEvent* rawEvent) {
262 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700263 switch (rawEvent->type) {
264 case EV_ABS: {
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500265 auto it = mAxes.find(rawEvent->code);
266 if (it != mAxes.end()) {
267 Axis& axis = it->second;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700268 float newValue, highNewValue;
269 switch (axis.axisInfo.mode) {
270 case AxisInfo::MODE_INVERT:
271 newValue = (axis.rawAxisInfo.maxValue - rawEvent->value) * axis.scale +
272 axis.offset;
273 highNewValue = 0.0f;
274 break;
275 case AxisInfo::MODE_SPLIT:
276 if (rawEvent->value < axis.axisInfo.splitValue) {
277 newValue = (axis.axisInfo.splitValue - rawEvent->value) * axis.scale +
278 axis.offset;
279 highNewValue = 0.0f;
280 } else if (rawEvent->value > axis.axisInfo.splitValue) {
281 newValue = 0.0f;
282 highNewValue =
283 (rawEvent->value - axis.axisInfo.splitValue) * axis.highScale +
284 axis.highOffset;
285 } else {
286 newValue = 0.0f;
287 highNewValue = 0.0f;
288 }
289 break;
290 default:
291 newValue = rawEvent->value * axis.scale + axis.offset;
292 highNewValue = 0.0f;
293 break;
294 }
295 axis.newValue = newValue;
296 axis.highNewValue = highNewValue;
297 }
298 break;
299 }
300
301 case EV_SYN:
302 switch (rawEvent->code) {
303 case SYN_REPORT:
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700304 out += sync(rawEvent->when, rawEvent->readTime, false /*force*/);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700305 break;
306 }
307 break;
308 }
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700309 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700310}
311
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700312std::list<NotifyArgs> JoystickInputMapper::sync(nsecs_t when, nsecs_t readTime, bool force) {
313 std::list<NotifyArgs> out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700314 if (!filterAxes(force)) {
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700315 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700316 }
317
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800318 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700319 int32_t buttonState = 0;
320
321 PointerProperties pointerProperties;
322 pointerProperties.clear();
323 pointerProperties.id = 0;
324 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
325
326 PointerCoords pointerCoords;
327 pointerCoords.clear();
328
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500329 for (std::pair<const int32_t, Axis>& pair : mAxes) {
330 const Axis& axis = pair.second;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700331 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue);
332 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
333 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis,
334 axis.highCurrentValue);
335 }
336 }
337
338 // Moving a joystick axis should not wake the device because joysticks can
339 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
340 // button will likely wake the device.
341 // TODO: Use the input device configuration to control this behavior more finely.
342 uint32_t policyFlags = 0;
Arthur Hung6d5b4b22022-01-21 07:21:10 +0000343 int32_t displayId = ADISPLAY_ID_NONE;
344 if (getDeviceContext().getAssociatedViewport()) {
345 displayId = getDeviceContext().getAssociatedViewport()->displayId;
346 }
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700347
Siarhei Vishniakou2935db72022-09-22 13:35:22 -0700348 out.push_back(NotifyMotionArgs(getContext()->getNextId(), when, readTime, getDeviceId(),
349 AINPUT_SOURCE_JOYSTICK, displayId, policyFlags,
350 AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState,
351 MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
352 &pointerProperties, &pointerCoords, 0, 0,
353 AMOTION_EVENT_INVALID_CURSOR_POSITION,
354 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {}));
355 return out;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700356}
357
358void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
359 float value) {
360 pointerCoords->setAxisValue(axis, value);
361 /* In order to ease the transition for developers from using the old axes
362 * to the newer, more semantically correct axes, we'll continue to produce
363 * values for the old axes as mirrors of the value of their corresponding
364 * new axes. */
365 int32_t compatAxis = getCompatAxis(axis);
366 if (compatAxis >= 0) {
367 pointerCoords->setAxisValue(compatAxis, value);
368 }
369}
370
371bool JoystickInputMapper::filterAxes(bool force) {
372 bool atLeastOneSignificantChange = force;
Siarhei Vishniakoub4d960d2019-10-03 15:38:44 -0500373 for (std::pair<const int32_t, Axis>& pair : mAxes) {
374 Axis& axis = pair.second;
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700375 if (force ||
376 hasValueChangedSignificantly(axis.filter, axis.newValue, axis.currentValue, axis.min,
377 axis.max)) {
378 axis.currentValue = axis.newValue;
379 atLeastOneSignificantChange = true;
380 }
381 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
382 if (force ||
383 hasValueChangedSignificantly(axis.filter, axis.highNewValue, axis.highCurrentValue,
384 axis.min, axis.max)) {
385 axis.highCurrentValue = axis.highNewValue;
386 atLeastOneSignificantChange = true;
387 }
388 }
389 }
390 return atLeastOneSignificantChange;
391}
392
393bool JoystickInputMapper::hasValueChangedSignificantly(float filter, float newValue,
394 float currentValue, float min, float max) {
395 if (newValue != currentValue) {
396 // Filter out small changes in value unless the value is converging on the axis
397 // bounds or center point. This is intended to reduce the amount of information
398 // sent to applications by particularly noisy joysticks (such as PS3).
399 if (fabs(newValue - currentValue) > filter ||
400 hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) ||
401 hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) ||
402 hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
403 return true;
404 }
405 }
406 return false;
407}
408
409bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
410 float currentValue,
411 float thresholdValue) {
412 float newDistance = fabs(newValue - thresholdValue);
413 if (newDistance < filter) {
414 float oldDistance = fabs(currentValue - thresholdValue);
415 if (newDistance < oldDistance) {
416 return true;
417 }
418 }
419 return false;
420}
421
422} // namespace android