Code layout cleanup

- Fwd declare where possible
- List .h first in the .cpp to verify proper includes
- Remove hacky -internal.h file and move testBitInRange to a new component

Change-Id: I442248c4b32738c6c2af250f45d4c8822c862e08
This commit is contained in:
Tim Kilbourn
2015-05-19 15:04:30 -07:00
parent 4f3145d75f
commit dbc8c16841
17 changed files with 221 additions and 143 deletions

View File

@@ -18,6 +18,7 @@ LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := \
BitUtils.cpp \
InputHub.cpp \
InputDevice.cpp \
InputDeviceManager.cpp \

View File

@@ -0,0 +1,76 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define LOG_TAG "BitUtils"
//#define LOG_NDEBUG 0
#include "BitUtils.h"
#include <utils/Log.h>
// Enables debug output for hasKeyInRange
#define DEBUG_KEY_RANGE 0
namespace android {
#if DEBUG_KEY_RANGE
static const char* bitstrings[16] = {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};
#endif
bool testBitInRange(const uint8_t arr[], size_t start, size_t end) {
#if DEBUG_KEY_RANGE
ALOGD("testBitInRange(%d, %d)", start, end);
#endif
// Invalid range! This is nonsense; just say no.
if (end <= start) return false;
// Find byte array indices. The end is not included in the range, nor is
// endIndex. Round up for endIndex.
size_t startIndex = start / 8;
size_t endIndex = (end + 7) / 8;
#if DEBUG_KEY_RANGE
ALOGD("startIndex=%d, endIndex=%d", startIndex, endIndex);
#endif
for (size_t i = startIndex; i < endIndex; ++i) {
uint8_t bits = arr[i];
uint8_t mask = 0xff;
#if DEBUG_KEY_RANGE
ALOGD("block %04d: %s%s", i, bitstrings[bits >> 4], bitstrings[bits & 0x0f]);
#endif
if (bits) {
// Mask off bits before our start bit
if (i == startIndex) {
mask &= 0xff << (start % 8);
}
// Mask off bits after our end bit
if (i == endIndex - 1 && (end % 8)) {
mask &= 0xff >> (8 - (end % 8));
}
#if DEBUG_KEY_RANGE
ALOGD("mask: %s%s", bitstrings[mask >> 4], bitstrings[mask & 0x0f]);
#endif
// Test the index against the mask
if (bits & mask) return true;
}
}
return false;
}
} // namespace android

View File

@@ -14,16 +14,16 @@
* limitations under the License.
*/
#ifndef ANDROID_INPUT_HUB_INTERNAL_H_
#define ANDROID_INPUT_HUB_INTERNAL_H_
#ifndef ANDROID_BIT_UTILS_H_
#define ANDROID_BIT_UTILS_H_
#include <cstdint>
namespace android {
namespace internal {
/** Test whether any bits in the interval [start, end) are set in the array. */
bool testBitInRange(const uint8_t arr[], size_t start, size_t end);
} // namespace internal
} // namespace android
#endif // ANDROID_INPUT_HUB_INTERNAL_H_
#endif // ANDROID_BIT_UTILS_H_

View File

@@ -20,6 +20,8 @@
// Enables debug output for processing input events
#define DEBUG_INPUT_EVENTS 0
#include "InputDevice.h"
#include <linux/input.h>
#define __STDC_FORMAT_MACROS
@@ -30,9 +32,8 @@
#include <utils/Log.h>
#include <utils/Timers.h>
#include "InputHost.h"
#include "InputHub.h"
#include "InputDevice.h"
#include "InputMapper.h"
#include "SwitchInputMapper.h"
#define MSC_ANDROID_TIME_SEC 0x6

View File

@@ -22,12 +22,20 @@
#include <utils/Timers.h>
#include "InputHost.h"
#include "InputHub.h"
#include "InputMapper.h"
struct input_device_handle;
struct input_device_identifier;
namespace android {
class InputDeviceDefinition;
class InputDeviceNode;
class InputHostInterface;
struct InputEvent;
using InputDeviceHandle = struct input_device_handle;
using InputDeviceIdentifier = struct input_device_identifier;
/**
* InputDeviceInterface represents an input device in the HAL. It processes
* input events before passing them to the input host.

View File

@@ -17,10 +17,11 @@
#define LOG_TAG "InputDeviceManager"
//#define LOG_NDEBUG 0
#include "InputDeviceManager.h"
#include <utils/Log.h>
#include "InputDevice.h"
#include "InputDeviceManager.h"
namespace android {

View File

@@ -22,12 +22,13 @@
#include <utils/Timers.h>
#include "InputDevice.h"
#include "InputHost.h"
#include "InputHub.h"
namespace android {
class InputDeviceInterface;
class InputHostInterface;
/**
* InputDeviceManager keeps the mapping of InputDeviceNodes to
* InputDeviceInterfaces and handles the callbacks from the InputHub, delegating

View File

@@ -17,8 +17,7 @@
#define LOG_TAG "InputHub"
//#define LOG_NDEBUG 0
// Enables debug output for hasKeyInRange
#define DEBUG_KEY_RANGE 0
#include "InputHub.h"
#include <dirent.h>
#include <errno.h>
@@ -36,15 +35,14 @@
#include <vector>
#include "InputHub.h"
#include "InputHub-internal.h"
#include <android/input.h>
#include <hardware_legacy/power.h>
#include <linux/input.h>
#include <utils/Log.h>
#include "BitUtils.h"
namespace android {
static const char WAKE_LOCK_ID[] = "KeyEvents";
@@ -60,57 +58,6 @@ static constexpr size_t sizeofBitArray(size_t bits) {
return (bits + 7) / 8;
}
namespace internal {
#if DEBUG_KEY_RANGE
static const char* bitstrings[16] = {
"0000", "0001", "0010", "0011",
"0100", "0101", "0110", "0111",
"1000", "1001", "1010", "1011",
"1100", "1101", "1110", "1111",
};
#endif
bool testBitInRange(const uint8_t arr[], size_t start, size_t end) {
#if DEBUG_KEY_RANGE
ALOGD("testBitInRange(%d, %d)", start, end);
#endif
// Invalid range! This is nonsense; just say no.
if (end <= start) return false;
// Find byte array indices. The end is not included in the range, nor is
// endIndex. Round up for endIndex.
size_t startIndex = start / 8;
size_t endIndex = (end + 7) / 8;
#if DEBUG_KEY_RANGE
ALOGD("startIndex=%d, endIndex=%d", startIndex, endIndex);
#endif
for (size_t i = startIndex; i < endIndex; ++i) {
uint8_t bits = arr[i];
uint8_t mask = 0xff;
#if DEBUG_KEY_RANGE
ALOGD("block %04d: %s%s", i, bitstrings[bits >> 4], bitstrings[bits & 0x0f]);
#endif
if (bits) {
// Mask off bits before our start bit
if (i == startIndex) {
mask &= 0xff << (start % 8);
}
// Mask off bits after our end bit
if (i == endIndex - 1 && (end % 8)) {
mask &= 0xff >> (8 - (end % 8));
}
#if DEBUG_KEY_RANGE
ALOGD("mask: %s%s", bitstrings[mask >> 4], bitstrings[mask & 0x0f]);
#endif
// Test the index against the mask
if (bits & mask) return true;
}
}
return false;
}
} // namespace internal
static void getLinuxRelease(int* major, int* minor) {
struct utsname info;
if (uname(&info) || sscanf(info.release, "%d.%d", major, minor) <= 0) {
@@ -331,7 +278,7 @@ bool EvdevDeviceNode::hasKey(int32_t key) const {
}
bool EvdevDeviceNode::hasKeyInRange(int32_t startKey, int32_t endKey) const {
return internal::testBitInRange(mKeyBitmask, startKey, endKey);
return testBitInRange(mKeyBitmask, startKey, endKey);
}
bool EvdevDeviceNode::hasRelativeAxis(int axis) const {

View File

@@ -16,6 +16,8 @@
#include "InputMapper.h"
#include "InputHost.h"
namespace android {
InputReport* InputMapper::getInputReport() {

View File

@@ -17,11 +17,16 @@
#ifndef ANDROID_INPUT_MAPPER_H_
#define ANDROID_INPUT_MAPPER_H_
#include "InputHost.h"
#include "InputHub.h"
struct input_device_handle;
namespace android {
class InputDeviceNode;
class InputReport;
class InputReportDefinition;
struct InputEvent;
using InputDeviceHandle = struct input_device_handle;
/**
* An InputMapper processes raw evdev input events and combines them into
* Android input HAL reports. A given InputMapper will focus on a particular
@@ -31,7 +36,8 @@ namespace android {
*/
class InputMapper {
public:
virtual ~InputMapper() = default;
InputMapper() = default;
virtual ~InputMapper() {}
/**
* If the mapper supports input events from the InputDevice,

View File

@@ -5,6 +5,7 @@ LOCAL_C_INCLUDES += hardware/libhardware/modules/input/evdev
LOCAL_C_INCLUDES += $(TOP)/external/gmock/include
LOCAL_SRC_FILES:= \
BitUtils_test.cpp \
InputDevice_test.cpp \
InputHub_test.cpp \
InputMocks.cpp \

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "BitUtils.h"
#include <gtest/gtest.h>
namespace android {
namespace tests {
TEST(BitInRange, testInvalidRange) {
uint8_t arr[2] = { 0xff, 0xff };
EXPECT_FALSE(testBitInRange(arr, 0, 0));
EXPECT_FALSE(testBitInRange(arr, 1, 0));
}
TEST(BitInRange, testNoBits) {
uint8_t arr[1];
arr[0] = 0;
EXPECT_FALSE(testBitInRange(arr, 0, 8));
}
TEST(BitInRange, testOneBit) {
uint8_t arr[1];
for (int i = 0; i < 8; ++i) {
arr[0] = 1 << i;
EXPECT_TRUE(testBitInRange(arr, 0, 8));
}
}
TEST(BitInRange, testZeroStart) {
uint8_t arr[1] = { 0x10 };
for (int i = 0; i < 5; ++i) {
EXPECT_FALSE(testBitInRange(arr, 0, i));
}
for (int i = 5; i <= 8; ++i) {
EXPECT_TRUE(testBitInRange(arr, 0, i));
}
}
TEST(BitInRange, testByteBoundaryEnd) {
uint8_t arr[1] = { 0x10 };
for (int i = 0; i < 5; ++i) {
EXPECT_TRUE(testBitInRange(arr, i, 8));
}
for (int i = 5; i <= 8; ++i) {
EXPECT_FALSE(testBitInRange(arr, i, 8));
}
}
TEST(BitInRange, testMultiByteArray) {
// bits set: 11 and 16
uint8_t arr[3] = { 0x00, 0x08, 0x01 };
for (int start = 0; start < 24; ++start) {
for (int end = start + 1; end <= 24; ++end) {
if (start > 16 || end <= 11 || (start > 11 && end <= 16)) {
EXPECT_FALSE(testBitInRange(arr, start, end))
<< "range = (" << start << ", " << end << ")";
} else {
EXPECT_TRUE(testBitInRange(arr, start, end))
<< "range = (" << start << ", " << end << ")";
}
}
}
}
} // namespace tests
} // namespace android

View File

@@ -14,8 +14,7 @@
* limitations under the License.
*/
#define LOG_TAG "InputHub_test"
//#define LOG_NDEBUG 0
#include "InputDevice.h"
#include <memory>
@@ -25,7 +24,6 @@
#include <utils/Timers.h>
#include "InputDevice.h"
#include "InputHub.h"
#include "InputMocks.h"
#include "MockInputHost.h"

View File

@@ -14,23 +14,19 @@
* limitations under the License.
*/
#define LOG_TAG "InputHub_test"
//#define LOG_NDEBUG 0
#include <linux/input.h>
#include "InputHub.h"
#include <chrono>
#include <memory>
#include <mutex>
#include <linux/input.h>
#include <gtest/gtest.h>
#include <utils/Log.h>
#include <utils/StopWatch.h>
#include <utils/Timers.h>
#include "InputHub.h"
#include "InputHub-internal.h"
#include "TestHelpers.h"
// # of milliseconds to fudge stopwatch measurements
@@ -257,63 +253,5 @@ TEST_F(InputHubTest, DISABLED_testCallbackOrder) {
EXPECT_TRUE(deviceCallbackFinished);
}
using internal::testBitInRange;
TEST(BitInRange, testInvalidRange) {
uint8_t arr[2] = { 0xff, 0xff };
EXPECT_FALSE(testBitInRange(arr, 0, 0));
EXPECT_FALSE(testBitInRange(arr, 1, 0));
}
TEST(BitInRange, testNoBits) {
uint8_t arr[1];
arr[0] = 0;
EXPECT_FALSE(testBitInRange(arr, 0, 8));
}
TEST(BitInRange, testOneBit) {
uint8_t arr[1];
for (int i = 0; i < 8; ++i) {
arr[0] = 1 << i;
EXPECT_TRUE(testBitInRange(arr, 0, 8));
}
}
TEST(BitInRange, testZeroStart) {
uint8_t arr[1] = { 0x10 };
for (int i = 0; i < 5; ++i) {
EXPECT_FALSE(testBitInRange(arr, 0, i));
}
for (int i = 5; i <= 8; ++i) {
EXPECT_TRUE(testBitInRange(arr, 0, i));
}
}
TEST(BitInRange, testByteBoundaryEnd) {
uint8_t arr[1] = { 0x10 };
for (int i = 0; i < 5; ++i) {
EXPECT_TRUE(testBitInRange(arr, i, 8));
}
for (int i = 5; i <= 8; ++i) {
EXPECT_FALSE(testBitInRange(arr, i, 8));
}
}
TEST(BitInRange, testMultiByteArray) {
// bits set: 11 and 16
uint8_t arr[3] = { 0x00, 0x08, 0x01 };
for (int start = 0; start < 24; ++start) {
for (int end = start + 1; end <= 24; ++end) {
if (start > 16 || end <= 11 || (start > 11 && end <= 16)) {
EXPECT_FALSE(testBitInRange(arr, start, end))
<< "range = (" << start << ", " << end << ")";
} else {
EXPECT_TRUE(testBitInRange(arr, start, end))
<< "range = (" << start << ", " << end << ")";
}
}
}
}
} // namespace tests
} // namespace android

View File

@@ -1,3 +1,19 @@
/*
* Copyright (C) 2015 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "InputMocks.h"
namespace android {

View File

@@ -17,9 +17,10 @@
#ifndef ANDROID_MOCK_INPUT_HOST_H_
#define ANDROID_MOCK_INPUT_HOST_H_
#include "InputHost.h"
#include "gmock/gmock.h"
#include "InputHost.h"
namespace android {
namespace tests {

View File

@@ -17,6 +17,8 @@
#define LOG_TAG "TestHelpers"
#define LOG_NDEBUG 0
#include "TestHelpers.h"
#include <dirent.h>
#include <fcntl.h>
#include <stdlib.h>
@@ -26,8 +28,6 @@
#include <utils/Log.h>
#include "TestHelpers.h"
namespace android {
static const char kTmpDirTemplate[] = "/data/local/tmp/XXXXXX";