Skip to content

Commit 6d85ea9

Browse files
Jens GulinJohan Redestig
authored andcommitted
Keep track of remaining fd when devices are removed
Sometimes the wrong fd was accessed when the device was addressed by device id. The earlier implementation assumed that two arrays were in sync but one of them was compacted when devices were removed. Instead of that dependency the device now keeps track of it's file descriptor. Change-Id: I2b8a793d76b89ab464ae830482b309fe86031671
1 parent dd1880e commit 6d85ea9

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

include/ui/EventHub.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ class EventHub : public RefBase
117117
uint8_t* keyBitmask;
118118
KeyLayoutMap* layoutMap;
119119
String8 keylayoutFilename;
120+
int fd;
120121
device_t* next;
121122

122123
device_t(int32_t _id, const char* _path, const char* name);

libs/ui/EventHub.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
#define ID_MASK 0x0000ffff
5858
#define SEQ_MASK 0x7fff0000
5959
#define SEQ_SHIFT 16
60-
#define id_to_index(id) ((id&ID_MASK)+1)
6160

6261
#ifndef ABS_MT_TOUCH_MAJOR
6362
#define ABS_MT_TOUCH_MAJOR 0x30 /* Major axis of touching ellipse */
@@ -84,7 +83,7 @@ static inline int max(int v1, int v2)
8483

8584
EventHub::device_t::device_t(int32_t _id, const char* _path, const char* name)
8685
: id(_id), path(_path), name(name), classes(0)
87-
, keyBitmask(NULL), layoutMap(new KeyLayoutMap()), next(NULL) {
86+
, keyBitmask(NULL), layoutMap(new KeyLayoutMap()), fd(-1), next(NULL) {
8887
}
8988

9089
EventHub::device_t::~device_t() {
@@ -143,11 +142,12 @@ int EventHub::getAbsoluteInfo(int32_t deviceId, int axis, int *outMinValue,
143142

144143
struct input_absinfo info;
145144

146-
if(ioctl(mFDs[id_to_index(device->id)].fd, EVIOCGABS(axis), &info)) {
145+
if(ioctl(device->fd, EVIOCGABS(axis), &info)) {
147146
LOGE("Error reading absolute controller %d for device %s fd %d\n",
148-
axis, device->name.string(), mFDs[id_to_index(device->id)].fd);
147+
axis, device->name.string(), device->fd);
149148
return -1;
150149
}
150+
151151
*outMinValue = info.minimum;
152152
*outMaxValue = info.maximum;
153153
*outFlat = info.flat;
@@ -178,8 +178,7 @@ int EventHub::getSwitchState(int32_t deviceId, int sw) const
178178
if (sw >= 0 && sw <= SW_MAX) {
179179
uint8_t sw_bitmask[(SW_MAX+7)/8];
180180
memset(sw_bitmask, 0, sizeof(sw_bitmask));
181-
if (ioctl(mFDs[id_to_index(device->id)].fd,
182-
EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
181+
if (ioctl(device->fd, EVIOCGSW(sizeof(sw_bitmask)), sw_bitmask) >= 0) {
183182
return test_bit(sw, sw_bitmask) ? 1 : 0;
184183
}
185184
}
@@ -202,8 +201,7 @@ int EventHub::getScancodeState(int32_t deviceId, int code) const
202201
if (code >= 0 && code <= KEY_MAX) {
203202
uint8_t key_bitmask[(KEY_MAX+7)/8];
204203
memset(key_bitmask, 0, sizeof(key_bitmask));
205-
if (ioctl(mFDs[id_to_index(device->id)].fd,
206-
EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
204+
if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
207205
return test_bit(code, key_bitmask) ? 1 : 0;
208206
}
209207
}
@@ -227,8 +225,7 @@ int EventHub::getKeycodeState(int32_t deviceId, int code) const
227225

228226
uint8_t key_bitmask[(KEY_MAX+7)/8];
229227
memset(key_bitmask, 0, sizeof(key_bitmask));
230-
if (ioctl(mFDs[id_to_index(device->id)].fd,
231-
EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
228+
if (ioctl(device->fd, EVIOCGKEY(sizeof(key_bitmask)), key_bitmask) >= 0) {
232229
#if 0
233230
for (size_t i=0; i<=KEY_MAX; i++) {
234231
LOGI("(Scan code %d: down=%d)", i, test_bit(i, key_bitmask));
@@ -599,6 +596,7 @@ int EventHub::open_device(const char *deviceName)
599596
return -1;
600597
}
601598

599+
device->fd = fd;
602600
mFDs[mFDCount].fd = fd;
603601
mFDs[mFDCount].events = POLLIN;
604602

0 commit comments

Comments
 (0)