Enable -Wshadow for input code
To avoid common programming mistakes, enable -Wshadow for input code.
This requires several refactors, such as using range-based for loops and
avoiding repeating variable names that are already in scope.
To allow for -Wshadow, we convert to unordered_map and use a range-based
loop.
Bug: 142017994
Test: atest libinput_tests inputflinger_tests
Change-Id: Icb65045d3322eccc312bca63287e38897d9f9ff6
diff --git a/services/inputflinger/Android.bp b/services/inputflinger/Android.bp
index f67c9d0..f685628 100644
--- a/services/inputflinger/Android.bp
+++ b/services/inputflinger/Android.bp
@@ -21,6 +21,7 @@
"-Werror",
"-Wno-unused-parameter",
"-Wthread-safety",
+ "-Wshadow",
],
}
diff --git a/services/inputflinger/dispatcher/InputDispatcher.cpp b/services/inputflinger/dispatcher/InputDispatcher.cpp
index 49f9c54..14f7caf 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.cpp
+++ b/services/inputflinger/dispatcher/InputDispatcher.cpp
@@ -3864,9 +3864,8 @@
if (!mFocusedWindowHandlesByDisplay.empty()) {
ALOGE("But another display has a focused window:");
for (auto& it : mFocusedWindowHandlesByDisplay) {
- const int32_t displayId = it.first;
const sp<InputWindowHandle>& windowHandle = it.second;
- ALOGE("Display #%" PRId32 " has focused window: '%s'\n", displayId,
+ ALOGE("Display #%" PRId32 " has focused window: '%s'\n", it.first,
windowHandle->getName().c_str());
}
}
diff --git a/services/inputflinger/reader/EventHub.cpp b/services/inputflinger/reader/EventHub.cpp
index a1514af..18fcaed 100644
--- a/services/inputflinger/reader/EventHub.cpp
+++ b/services/inputflinger/reader/EventHub.cpp
@@ -933,11 +933,11 @@
if (eventItem.events & EPOLLIN) {
ALOGV("awoken after wake()");
awoken = true;
- char buffer[16];
+ char wakeReadBuffer[16];
ssize_t nRead;
do {
- nRead = read(mWakeReadPipeFd, buffer, sizeof(buffer));
- } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(buffer));
+ nRead = read(mWakeReadPipeFd, wakeReadBuffer, sizeof(wakeReadBuffer));
+ } while ((nRead == -1 && errno == EINTR) || nRead == sizeof(wakeReadBuffer));
} else {
ALOGW("Received unexpected epoll event 0x%08x for wake read pipe.",
eventItem.events);
diff --git a/services/inputflinger/reader/mapper/JoystickInputMapper.cpp b/services/inputflinger/reader/mapper/JoystickInputMapper.cpp
index b2e10d6..bf81496 100644
--- a/services/inputflinger/reader/mapper/JoystickInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/JoystickInputMapper.cpp
@@ -32,8 +32,8 @@
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,9 +72,7 @@
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);
+ for (const auto& [rawAxis, axis] : mAxes) {
const char* label = getAxisLabel(axis.axisInfo.axis);
if (label) {
dump += StringPrintf(INDENT4 "%s", label);
@@ -100,7 +98,7 @@
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);
}
@@ -130,9 +128,7 @@
axisInfo.mode = AxisInfo::MODE_NORMAL;
axisInfo.axis = -1;
}
-
- Axis axis = createAxis(axisInfo, rawAxisInfo, explicitlyMapped);
- mAxes.add(abs, axis);
+ mAxes.insert({abs, createAxis(axisInfo, rawAxisInfo, explicitlyMapped)});
}
}
@@ -147,9 +143,8 @@
// 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)) {
@@ -162,11 +157,12 @@
} 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++;
}
}
}
@@ -177,38 +173,41 @@
// 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.
- 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);
+ scale = 1.0f / (axisInfo.splitValue - rawAxisInfo.minValue);
+ highScale = 1.0f / (rawAxisInfo.maxValue - axisInfo.splitValue);
} 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);
+ scale = 2.0f / (rawAxisInfo.maxValue - rawAxisInfo.minValue);
+ offset = avg(rawAxisInfo.minValue, rawAxisInfo.maxValue) * -scale;
+ highOffset = offset;
+ highScale = scale;
+ min = -1.0f;
} 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);
+ 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.
- axis.filter = axis.fuzz ? axis.fuzz : axis.flat * 0.25f;
-
- return axis;
+ 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<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;
@@ -218,14 +217,14 @@
}
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);
}
}
@@ -250,9 +249,8 @@
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();
}
@@ -262,9 +260,9 @@
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:
@@ -324,9 +322,8 @@
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,
@@ -364,9 +361,8 @@
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)) {
diff --git a/services/inputflinger/reader/mapper/JoystickInputMapper.h b/services/inputflinger/reader/mapper/JoystickInputMapper.h
index 29d5652..0cf60a2 100644
--- a/services/inputflinger/reader/mapper/JoystickInputMapper.h
+++ b/services/inputflinger/reader/mapper/JoystickInputMapper.h
@@ -36,6 +36,26 @@
private:
struct Axis {
+ explicit Axis(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
+ bool explicitlyMapped, float scale, float offset, float highScale,
+ float highOffset, float min, float max, float flat, float fuzz,
+ float resolution, float filter)
+ : rawAxisInfo(rawAxisInfo),
+ axisInfo(axisInfo),
+ explicitlyMapped(explicitlyMapped),
+ scale(scale),
+ offset(offset),
+ highScale(highScale),
+ highOffset(highOffset),
+ min(min),
+ max(max),
+ flat(flat),
+ fuzz(fuzz),
+ resolution(resolution),
+ filter(filter) {
+ resetValue();
+ }
+
RawAbsoluteAxisInfo rawAxisInfo;
AxisInfo axisInfo;
@@ -58,26 +78,6 @@
float highCurrentValue; // current value of high split
float highNewValue; // most recent value of high split
- void initialize(const RawAbsoluteAxisInfo& rawAxisInfo, const AxisInfo& axisInfo,
- bool explicitlyMapped, float scale, float offset, float highScale,
- float highOffset, float min, float max, float flat, float fuzz,
- float resolution) {
- this->rawAxisInfo = rawAxisInfo;
- this->axisInfo = axisInfo;
- this->explicitlyMapped = explicitlyMapped;
- this->scale = scale;
- this->offset = offset;
- this->highScale = highScale;
- this->highOffset = highOffset;
- this->min = min;
- this->max = max;
- this->flat = flat;
- this->fuzz = fuzz;
- this->resolution = resolution;
- this->filter = 0;
- resetValue();
- }
-
void resetValue() {
this->currentValue = 0;
this->newValue = 0;
@@ -90,7 +90,7 @@
bool explicitlyMapped);
// Axes indexed by raw ABS_* axis index.
- KeyedVector<int32_t, Axis> mAxes;
+ std::unordered_map<int32_t, Axis> mAxes;
void sync(nsecs_t when, bool force);