blob: b2e10d64cef100e59c04bcc84d0c30c04ab15c7b [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
28uint32_t JoystickInputMapper::getSources() {
29 return AINPUT_SOURCE_JOYSTICK;
30}
31
32void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
33 InputMapper::populateDeviceInfo(info);
34
35 for (size_t i = 0; i < mAxes.size(); i++) {
36 const Axis& axis = mAxes.valueAt(i);
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
45void 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);
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) {
53 info->addMotionRange(compatAxis, AINPUT_SOURCE_JOYSTICK, axis.min, axis.max, axis.flat,
54 axis.fuzz, axis.resolution);
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. */
61int32_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
71void JoystickInputMapper::dump(std::string& dump) {
72 dump += INDENT2 "Joystick Input Mapper:\n";
73
74 dump += INDENT3 "Axes:\n";
75 size_t numAxes = mAxes.size();
76 for (size_t i = 0; i < numAxes; i++) {
77 const Axis& axis = mAxes.valueAt(i);
78 const char* label = getAxisLabel(axis.axisInfo.axis);
79 if (label) {
80 dump += StringPrintf(INDENT4 "%s", label);
81 } else {
82 dump += StringPrintf(INDENT4 "%d", axis.axisInfo.axis);
83 }
84 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
85 label = getAxisLabel(axis.axisInfo.highAxis);
86 if (label) {
87 dump += StringPrintf(" / %s (split at %d)", label, axis.axisInfo.splitValue);
88 } else {
89 dump += StringPrintf(" / %d (split at %d)", axis.axisInfo.highAxis,
90 axis.axisInfo.splitValue);
91 }
92 } else if (axis.axisInfo.mode == AxisInfo::MODE_INVERT) {
93 dump += " (invert)";
94 }
95
96 dump += StringPrintf(": min=%0.5f, max=%0.5f, flat=%0.5f, fuzz=%0.5f, resolution=%0.5f\n",
97 axis.min, axis.max, axis.flat, axis.fuzz, axis.resolution);
98 dump += StringPrintf(INDENT4 " scale=%0.5f, offset=%0.5f, "
99 "highScale=%0.5f, highOffset=%0.5f\n",
100 axis.scale, axis.offset, axis.highScale, axis.highOffset);
101 dump += StringPrintf(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, "
102 "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n",
103 mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue,
104 axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz,
105 axis.rawAxisInfo.resolution);
106 }
107}
108
109void JoystickInputMapper::configure(nsecs_t when, const InputReaderConfiguration* config,
110 uint32_t changes) {
111 InputMapper::configure(when, config, changes);
112
113 if (!changes) { // first time only
114 // Collect all axes.
115 for (int32_t abs = 0; abs <= ABS_MAX; abs++) {
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800116 if (!(getAbsAxisUsage(abs, getDeviceContext().getDeviceClasses()) &
117 INPUT_DEVICE_CLASS_JOYSTICK)) {
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700118 continue; // axis must be claimed by a different device
119 }
120
121 RawAbsoluteAxisInfo rawAxisInfo;
122 getAbsoluteAxisInfo(abs, &rawAxisInfo);
123 if (rawAxisInfo.valid) {
124 // Map axis.
125 AxisInfo axisInfo;
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500126 const bool explicitlyMapped = !getDeviceContext().mapAxis(abs, &axisInfo);
127
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700128 if (!explicitlyMapped) {
129 // Axis is not explicitly mapped, will choose a generic axis later.
130 axisInfo.mode = AxisInfo::MODE_NORMAL;
131 axisInfo.axis = -1;
132 }
133
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500134 Axis axis = createAxis(axisInfo, rawAxisInfo, explicitlyMapped);
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700135 mAxes.add(abs, axis);
136 }
137 }
138
139 // If there are too many axes, start dropping them.
140 // Prefer to keep explicitly mapped axes.
141 if (mAxes.size() > PointerCoords::MAX_AXES) {
142 ALOGI("Joystick '%s' has %zu axes but the framework only supports a maximum of %d.",
143 getDeviceName().c_str(), mAxes.size(), PointerCoords::MAX_AXES);
144 pruneAxes(true);
145 pruneAxes(false);
146 }
147
148 // Assign generic axis ids to remaining axes.
149 int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1;
150 size_t numAxes = mAxes.size();
151 for (size_t i = 0; i < numAxes; i++) {
152 Axis& axis = mAxes.editValueAt(i);
153 if (axis.axisInfo.axis < 0) {
154 while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 &&
155 haveAxis(nextGenericAxisId)) {
156 nextGenericAxisId += 1;
157 }
158
159 if (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16) {
160 axis.axisInfo.axis = nextGenericAxisId;
161 nextGenericAxisId += 1;
162 } else {
163 ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids "
164 "have already been assigned to other axes.",
165 getDeviceName().c_str(), mAxes.keyAt(i));
166 mAxes.removeItemsAt(i--);
167 numAxes -= 1;
168 }
169 }
170 }
171 }
172}
173
Siarhei Vishniakou690f8be2020-06-30 04:31:25 -0500174JoystickInputMapper::Axis JoystickInputMapper::createAxis(const AxisInfo& axisInfo,
175 const RawAbsoluteAxisInfo& rawAxisInfo,
176 bool explicitlyMapped) {
177 // Apply flat override.
178 int32_t rawFlat = axisInfo.flatOverride < 0 ? rawAxisInfo.flat : axisInfo.flatOverride;
179
180 // Calculate scaling factors and limits.
181 Axis axis;
182 if (axisInfo.mode == AxisInfo::MODE_SPLIT) {
183 float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
184 float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
185 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, scale, 0.0f, highScale, 0.0f, 0.0f,
186 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
187 rawAxisInfo.resolution * scale);
188 } else if (isCenteredAxis(axisInfo.axis)) {
189 float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
190 float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
191 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, scale, offset, scale, offset,
192 -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
193 rawAxisInfo.resolution * scale);
194 } else {
195 float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
196 axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, scale, 0.0f, scale, 0.0f, 0.0f,
197 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale,
198 rawAxisInfo.resolution * scale);
199 }
200
201 // To eliminate noise while the joystick is at rest, filter out small variations
202 // in axis values up front.
203 axis.filter = axis.fuzz ? axis.fuzz : axis.flat * 0.25f;
204
205 return axis;
206}
207
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700208bool JoystickInputMapper::haveAxis(int32_t axisId) {
209 size_t numAxes = mAxes.size();
210 for (size_t i = 0; i < numAxes; i++) {
211 const Axis& axis = mAxes.valueAt(i);
212 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) {
221 size_t i = mAxes.size();
222 while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) {
223 if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) {
224 continue;
225 }
226 ALOGI("Discarding joystick '%s' axis %d because there are too many axes.",
227 getDeviceName().c_str(), mAxes.keyAt(i));
228 mAxes.removeItemsAt(i);
229 }
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
251void JoystickInputMapper::reset(nsecs_t when) {
252 // Recenter all axes.
253 size_t numAxes = mAxes.size();
254 for (size_t i = 0; i < numAxes; i++) {
255 Axis& axis = mAxes.editValueAt(i);
256 axis.resetValue();
257 }
258
259 InputMapper::reset(when);
260}
261
262void JoystickInputMapper::process(const RawEvent* rawEvent) {
263 switch (rawEvent->type) {
264 case EV_ABS: {
265 ssize_t index = mAxes.indexOfKey(rawEvent->code);
266 if (index >= 0) {
267 Axis& axis = mAxes.editValueAt(index);
268 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:
304 sync(rawEvent->when, false /*force*/);
305 break;
306 }
307 break;
308 }
309}
310
311void JoystickInputMapper::sync(nsecs_t when, bool force) {
312 if (!filterAxes(force)) {
313 return;
314 }
315
Nathaniel R. Lewis26ec2222020-01-10 16:30:54 -0800316 int32_t metaState = getContext()->getGlobalMetaState();
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700317 int32_t buttonState = 0;
318
319 PointerProperties pointerProperties;
320 pointerProperties.clear();
321 pointerProperties.id = 0;
322 pointerProperties.toolType = AMOTION_EVENT_TOOL_TYPE_UNKNOWN;
323
324 PointerCoords pointerCoords;
325 pointerCoords.clear();
326
327 size_t numAxes = mAxes.size();
328 for (size_t i = 0; i < numAxes; i++) {
329 const Axis& axis = mAxes.valueAt(i);
330 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue);
331 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
332 setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis,
333 axis.highCurrentValue);
334 }
335 }
336
337 // Moving a joystick axis should not wake the device because joysticks can
338 // be fairly noisy even when not in use. On the other hand, pushing a gamepad
339 // button will likely wake the device.
340 // TODO: Use the input device configuration to control this behavior more finely.
341 uint32_t policyFlags = 0;
342
Garfield Tan6a5a14e2020-01-28 13:24:04 -0800343 NotifyMotionArgs args(getContext()->getNextId(), when, getDeviceId(), AINPUT_SOURCE_JOYSTICK,
344 ADISPLAY_ID_NONE, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState,
345 buttonState, MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1,
Prabir Pradhanbaa5c822019-08-30 15:27:05 -0700346 &pointerProperties, &pointerCoords, 0, 0,
347 AMOTION_EVENT_INVALID_CURSOR_POSITION,
348 AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {});
349 getListener()->notifyMotion(&args);
350}
351
352void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords, int32_t axis,
353 float value) {
354 pointerCoords->setAxisValue(axis, value);
355 /* In order to ease the transition for developers from using the old axes
356 * to the newer, more semantically correct axes, we'll continue to produce
357 * values for the old axes as mirrors of the value of their corresponding
358 * new axes. */
359 int32_t compatAxis = getCompatAxis(axis);
360 if (compatAxis >= 0) {
361 pointerCoords->setAxisValue(compatAxis, value);
362 }
363}
364
365bool JoystickInputMapper::filterAxes(bool force) {
366 bool atLeastOneSignificantChange = force;
367 size_t numAxes = mAxes.size();
368 for (size_t i = 0; i < numAxes; i++) {
369 Axis& axis = mAxes.editValueAt(i);
370 if (force ||
371 hasValueChangedSignificantly(axis.filter, axis.newValue, axis.currentValue, axis.min,
372 axis.max)) {
373 axis.currentValue = axis.newValue;
374 atLeastOneSignificantChange = true;
375 }
376 if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) {
377 if (force ||
378 hasValueChangedSignificantly(axis.filter, axis.highNewValue, axis.highCurrentValue,
379 axis.min, axis.max)) {
380 axis.highCurrentValue = axis.highNewValue;
381 atLeastOneSignificantChange = true;
382 }
383 }
384 }
385 return atLeastOneSignificantChange;
386}
387
388bool JoystickInputMapper::hasValueChangedSignificantly(float filter, float newValue,
389 float currentValue, float min, float max) {
390 if (newValue != currentValue) {
391 // Filter out small changes in value unless the value is converging on the axis
392 // bounds or center point. This is intended to reduce the amount of information
393 // sent to applications by particularly noisy joysticks (such as PS3).
394 if (fabs(newValue - currentValue) > filter ||
395 hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, min) ||
396 hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, max) ||
397 hasMovedNearerToValueWithinFilteredRange(filter, newValue, currentValue, 0)) {
398 return true;
399 }
400 }
401 return false;
402}
403
404bool JoystickInputMapper::hasMovedNearerToValueWithinFilteredRange(float filter, float newValue,
405 float currentValue,
406 float thresholdValue) {
407 float newDistance = fabs(newValue - thresholdValue);
408 if (newDistance < filter) {
409 float oldDistance = fabs(currentValue - thresholdValue);
410 if (newDistance < oldDistance) {
411 return true;
412 }
413 }
414 return false;
415}
416
417} // namespace android