From 5aa3d79025c6565ee274945a032a269fc86db85f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C3=ABl=20Burtin?= Date: Fri, 29 Jul 2011 14:25:47 +0200 Subject: [PATCH] Fix getSwitchState according to device capabilities. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When querying switch state for a given device id, ensure that the device exposes the given switch in its capabilities, report AKEY_STATE_UNKNOWN otherwise. This fix a bug in InputManager that reports an incorrect switch state (down) when a device exposes at least one switch in its capabilites and another switch is queried. For example, this can leads in always reporting LID state open (SW_LID down) if only SW_HEADPHONE_INSERT is exposed in capabilities. Change-Id: I4e5265ec02af918c317673789e7948529842aa2d Signed-off-by: Michaƫl Burtin --- include/ui/EventHub.h | 1 + libs/ui/EventHub.cpp | 25 +++++++++++++++++++------ 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/include/ui/EventHub.h b/include/ui/EventHub.h index d78e35fbf..57a3304f4 100644 --- a/include/ui/EventHub.h +++ b/include/ui/EventHub.h @@ -239,6 +239,7 @@ private: String8 name; uint32_t classes; uint8_t* keyBitmask; + uint8_t* switchBitmask; KeyLayoutMap* layoutMap; String8 keylayoutFilename; int fd; diff --git a/libs/ui/EventHub.cpp b/libs/ui/EventHub.cpp index 41daa9ca0..4302678f2 100644 --- a/libs/ui/EventHub.cpp +++ b/libs/ui/EventHub.cpp @@ -94,11 +94,13 @@ static inline const char* toString(bool value) { EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name) : id(_id), path(_path), name(name), classes(0) - , keyBitmask(NULL), layoutMap(new KeyLayoutMap()), fd(-1), next(NULL) { + , keyBitmask(NULL), switchBitmask(NULL) + , layoutMap(new KeyLayoutMap()), fd(-1), next(NULL) { } EventHub::device_t::~device_t() { delete [] keyBitmask; + delete [] switchBitmask; delete layoutMap; } @@ -243,11 +245,14 @@ int32_t EventHub::getSwitchState(int32_t deviceId, int32_t sw) const { } int32_t EventHub::getSwitchStateLocked(device_t* device, int32_t sw) const { - uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)]; - memset(sw_bitmask, 0, sizeof(sw_bitmask)); - if (ioctl(device->fd, - EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) { - return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; + if (device->switchBitmask != NULL + && test_bit(sw, device->switchBitmask)) { + uint8_t sw_bitmask[sizeof_bit_array(SW_MAX + 1)]; + memset(sw_bitmask, 0, sizeof(sw_bitmask)); + if (ioctl(device->fd, + EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) { + return test_bit(sw, sw_bitmask) ? AKEY_STATE_DOWN : AKEY_STATE_UP; + } } return AKEY_STATE_UNKNOWN; } @@ -759,6 +764,14 @@ int EventHub::openDevice(const char *deviceName) { } if (hasSwitches) { device->classes |= INPUT_DEVICE_CLASS_SWITCH; + device->switchBitmask = new uint8_t[sizeof(sw_bitmask)]; + if (device->switchBitmask != NULL) { + memcpy(device->switchBitmask, sw_bitmask, sizeof(sw_bitmask)); + } else { + delete device; + LOGE("out of memory allocating switch bitmask"); + return -1; + } } #endif