diff options
Diffstat (limited to 'services/inputflinger/reader/mapper/JoystickInputMapper.cpp')
-rw-r--r-- | services/inputflinger/reader/mapper/JoystickInputMapper.cpp | 150 |
1 files changed, 77 insertions, 73 deletions
diff --git a/services/inputflinger/reader/mapper/JoystickInputMapper.cpp b/services/inputflinger/reader/mapper/JoystickInputMapper.cpp index 030a846727..0dc312ecd2 100644 --- a/services/inputflinger/reader/mapper/JoystickInputMapper.cpp +++ b/services/inputflinger/reader/mapper/JoystickInputMapper.cpp @@ -32,8 +32,8 @@ uint32_t JoystickInputMapper::getSources() { void JoystickInputMapper::populateDeviceInfo(InputDeviceInfo* info) { InputMapper::populateDeviceInfo(info); - for (size_t i = 0; i < mAxes.size(); i++) { - const Axis& axis = mAxes.valueAt(i); + for (std::pair<const int32_t, Axis>& pair : mAxes) { + const Axis& axis = pair.second; addMotionRange(axis.axisInfo.axis, axis, info); if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { @@ -72,17 +72,15 @@ void JoystickInputMapper::dump(std::string& dump) { dump += INDENT2 "Joystick Input Mapper:\n"; dump += INDENT3 "Axes:\n"; - size_t numAxes = mAxes.size(); - for (size_t i = 0; i < numAxes; i++) { - const Axis& axis = mAxes.valueAt(i); - const char* label = getAxisLabel(axis.axisInfo.axis); + for (const auto& [rawAxis, axis] : mAxes) { + const char* label = InputEventLookup::getAxisLabel(axis.axisInfo.axis); if (label) { dump += StringPrintf(INDENT4 "%s", label); } else { dump += StringPrintf(INDENT4 "%d", axis.axisInfo.axis); } if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { - label = getAxisLabel(axis.axisInfo.highAxis); + label = InputEventLookup::getAxisLabel(axis.axisInfo.highAxis); if (label) { dump += StringPrintf(" / %s (split at %d)", label, axis.axisInfo.splitValue); } else { @@ -100,7 +98,7 @@ void JoystickInputMapper::dump(std::string& dump) { axis.scale, axis.offset, axis.highScale, axis.highOffset); dump += StringPrintf(INDENT4 " rawAxis=%d, rawMin=%d, rawMax=%d, " "rawFlat=%d, rawFuzz=%d, rawResolution=%d\n", - mAxes.keyAt(i), axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, + rawAxis, axis.rawAxisInfo.minValue, axis.rawAxisInfo.maxValue, axis.rawAxisInfo.flat, axis.rawAxisInfo.fuzz, axis.rawAxisInfo.resolution); } @@ -113,8 +111,8 @@ void JoystickInputMapper::configure(nsecs_t when, const InputReaderConfiguration if (!changes) { // first time only // Collect all axes. for (int32_t abs = 0; abs <= ABS_MAX; abs++) { - if (!(getAbsAxisUsage(abs, getDeviceContext().getDeviceClasses()) & - INPUT_DEVICE_CLASS_JOYSTICK)) { + if (!(getAbsAxisUsage(abs, getDeviceContext().getDeviceClasses()) + .test(InputDeviceClass::JOYSTICK))) { continue; // axis must be claimed by a different device } @@ -123,43 +121,14 @@ void JoystickInputMapper::configure(nsecs_t when, const InputReaderConfiguration if (rawAxisInfo.valid) { // Map axis. AxisInfo axisInfo; - bool explicitlyMapped = !getDeviceContext().mapAxis(abs, &axisInfo); + const bool explicitlyMapped = !getDeviceContext().mapAxis(abs, &axisInfo); + if (!explicitlyMapped) { // Axis is not explicitly mapped, will choose a generic axis later. axisInfo.mode = AxisInfo::MODE_NORMAL; axisInfo.axis = -1; } - - // Apply flat override. - int32_t rawFlat = - axisInfo.flatOverride < 0 ? rawAxisInfo.flat : axisInfo.flatOverride; - - // Calculate scaling factors and limits. - Axis axis; - if (axisInfo.mode == AxisInfo::MODE_SPLIT) { - float scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); - float highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); - axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, scale, 0.0f, highScale, - 0.0f, 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, - rawAxisInfo.resolution * scale); - } else if (isCenteredAxis(axisInfo.axis)) { - float scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); - float offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; - axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, scale, offset, scale, - offset, -1.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, - rawAxisInfo.resolution * scale); - } else { - float scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); - axis.initialize(rawAxisInfo, axisInfo, explicitlyMapped, scale, 0.0f, scale, - 0.0f, 0.0f, 1.0f, rawFlat * scale, rawAxisInfo.fuzz * scale, - rawAxisInfo.resolution * scale); - } - - // To eliminate noise while the joystick is at rest, filter out small variations - // in axis values up front. - axis.filter = axis.fuzz ? axis.fuzz : axis.flat * 0.25f; - - mAxes.add(abs, axis); + mAxes.insert({abs, createAxis(axisInfo, rawAxisInfo, explicitlyMapped)}); } } @@ -174,9 +143,8 @@ void JoystickInputMapper::configure(nsecs_t when, const InputReaderConfiguration // Assign generic axis ids to remaining axes. int32_t nextGenericAxisId = AMOTION_EVENT_AXIS_GENERIC_1; - size_t numAxes = mAxes.size(); - for (size_t i = 0; i < numAxes; i++) { - Axis& axis = mAxes.editValueAt(i); + for (auto it = mAxes.begin(); it != mAxes.end(); /*increment it inside loop*/) { + Axis& axis = it->second; if (axis.axisInfo.axis < 0) { while (nextGenericAxisId <= AMOTION_EVENT_AXIS_GENERIC_16 && haveAxis(nextGenericAxisId)) { @@ -189,19 +157,57 @@ void JoystickInputMapper::configure(nsecs_t when, const InputReaderConfiguration } else { ALOGI("Ignoring joystick '%s' axis %d because all of the generic axis ids " "have already been assigned to other axes.", - getDeviceName().c_str(), mAxes.keyAt(i)); - mAxes.removeItemsAt(i--); - numAxes -= 1; + getDeviceName().c_str(), it->first); + it = mAxes.erase(it); + continue; } } + it++; } } } +JoystickInputMapper::Axis JoystickInputMapper::createAxis(const AxisInfo& axisInfo, + const RawAbsoluteAxisInfo& rawAxisInfo, + bool explicitlyMapped) { + // Apply flat override. + int32_t rawFlat = axisInfo.flatOverride < 0 ? rawAxisInfo.flat : axisInfo.flatOverride; + + float scale = std::numeric_limits<float>::signaling_NaN(); + float highScale = std::numeric_limits<float>::signaling_NaN(); + float highOffset = 0; + float offset = 0; + float min = 0; + // Calculate scaling factors and limits. + if (axisInfo.mode == AxisInfo::MODE_SPLIT) { + scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue); + highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue); + } else if (isCenteredAxis(axisInfo.axis)) { + scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); + offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale; + highOffset = offset; + highScale = scale; + min = -1.0f; + } else { + scale = 1.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue); + highScale = scale; + } + + constexpr float max = 1.0; + const float flat = rawFlat * scale; + const float fuzz = rawAxisInfo.fuzz * scale; + const float resolution = rawAxisInfo.resolution * scale; + + // To eliminate noise while the joystick is at rest, filter out small variations + // in axis values up front. + const float filter = fuzz ? fuzz : flat * 0.25f; + return Axis(rawAxisInfo, axisInfo, explicitlyMapped, scale, offset, highScale, highOffset, min, + max, flat, fuzz, resolution, filter); +} + bool JoystickInputMapper::haveAxis(int32_t axisId) { - size_t numAxes = mAxes.size(); - for (size_t i = 0; i < numAxes; i++) { - const Axis& axis = mAxes.valueAt(i); + for (const std::pair<const int32_t, Axis>& pair : mAxes) { + const Axis& axis = pair.second; if (axis.axisInfo.axis == axisId || (axis.axisInfo.mode == AxisInfo::MODE_SPLIT && axis.axisInfo.highAxis == axisId)) { return true; @@ -211,14 +217,14 @@ bool JoystickInputMapper::haveAxis(int32_t axisId) { } void JoystickInputMapper::pruneAxes(bool ignoreExplicitlyMappedAxes) { - size_t i = mAxes.size(); - while (mAxes.size() > PointerCoords::MAX_AXES && i-- > 0) { - if (ignoreExplicitlyMappedAxes && mAxes.valueAt(i).explicitlyMapped) { + while (mAxes.size() > PointerCoords::MAX_AXES) { + auto it = mAxes.begin(); + if (ignoreExplicitlyMappedAxes && it->second.explicitlyMapped) { continue; } ALOGI("Discarding joystick '%s' axis %d because there are too many axes.", - getDeviceName().c_str(), mAxes.keyAt(i)); - mAxes.removeItemsAt(i); + getDeviceName().c_str(), it->first); + mAxes.erase(it); } } @@ -243,9 +249,8 @@ bool JoystickInputMapper::isCenteredAxis(int32_t axis) { void JoystickInputMapper::reset(nsecs_t when) { // Recenter all axes. - size_t numAxes = mAxes.size(); - for (size_t i = 0; i < numAxes; i++) { - Axis& axis = mAxes.editValueAt(i); + for (std::pair<const int32_t, Axis>& pair : mAxes) { + Axis& axis = pair.second; axis.resetValue(); } @@ -255,9 +260,9 @@ void JoystickInputMapper::reset(nsecs_t when) { void JoystickInputMapper::process(const RawEvent* rawEvent) { switch (rawEvent->type) { case EV_ABS: { - ssize_t index = mAxes.indexOfKey(rawEvent->code); - if (index >= 0) { - Axis& axis = mAxes.editValueAt(index); + auto it = mAxes.find(rawEvent->code); + if (it != mAxes.end()) { + Axis& axis = it->second; float newValue, highNewValue; switch (axis.axisInfo.mode) { case AxisInfo::MODE_INVERT: @@ -294,14 +299,14 @@ void JoystickInputMapper::process(const RawEvent* rawEvent) { case EV_SYN: switch (rawEvent->code) { case SYN_REPORT: - sync(rawEvent->when, false /*force*/); + sync(rawEvent->when, rawEvent->readTime, false /*force*/); break; } break; } } -void JoystickInputMapper::sync(nsecs_t when, bool force) { +void JoystickInputMapper::sync(nsecs_t when, nsecs_t readTime, bool force) { if (!filterAxes(force)) { return; } @@ -317,9 +322,8 @@ void JoystickInputMapper::sync(nsecs_t when, bool force) { PointerCoords pointerCoords; pointerCoords.clear(); - size_t numAxes = mAxes.size(); - for (size_t i = 0; i < numAxes; i++) { - const Axis& axis = mAxes.valueAt(i); + for (std::pair<const int32_t, Axis>& pair : mAxes) { + const Axis& axis = pair.second; setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.axis, axis.currentValue); if (axis.axisInfo.mode == AxisInfo::MODE_SPLIT) { setPointerCoordsAxisValue(&pointerCoords, axis.axisInfo.highAxis, @@ -333,9 +337,10 @@ void JoystickInputMapper::sync(nsecs_t when, bool force) { // TODO: Use the input device configuration to control this behavior more finely. uint32_t policyFlags = 0; - NotifyMotionArgs args(getContext()->getNextId(), when, getDeviceId(), AINPUT_SOURCE_JOYSTICK, - ADISPLAY_ID_NONE, policyFlags, AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, - buttonState, MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, + NotifyMotionArgs args(getContext()->getNextId(), when, readTime, getDeviceId(), + AINPUT_SOURCE_JOYSTICK, ADISPLAY_ID_NONE, policyFlags, + AMOTION_EVENT_ACTION_MOVE, 0, 0, metaState, buttonState, + MotionClassification::NONE, AMOTION_EVENT_EDGE_FLAG_NONE, 1, &pointerProperties, &pointerCoords, 0, 0, AMOTION_EVENT_INVALID_CURSOR_POSITION, AMOTION_EVENT_INVALID_CURSOR_POSITION, 0, /* videoFrames */ {}); @@ -357,9 +362,8 @@ void JoystickInputMapper::setPointerCoordsAxisValue(PointerCoords* pointerCoords bool JoystickInputMapper::filterAxes(bool force) { bool atLeastOneSignificantChange = force; - size_t numAxes = mAxes.size(); - for (size_t i = 0; i < numAxes; i++) { - Axis& axis = mAxes.editValueAt(i); + for (std::pair<const int32_t, Axis>& pair : mAxes) { + Axis& axis = pair.second; if (force || hasValueChangedSignificantly(axis.filter, axis.newValue, axis.currentValue, axis.min, axis.max)) { |