android 6取消键盘振动,Android应用程序键盘(Keyboard)消息处理机制分析(6)

巢德华
2023-12-01

intEventHub::openDevice(constchar*deviceName) {

intversion;

intfd;

struct pollfd *new_mFDs;

device_t **new_devices;

char**new_device_names;

charname[80];

charlocation[80];

charidstr[80];

struct input_id id;

LOGV("Opening device: %s", deviceName);

AutoMutex _l(mLock);

fd = open(deviceName, O_RDWR);

if(fd 

LOGE("could not open %s, %s\n", deviceName, strerror(errno));

return-1;

}

......

intdevid = 0;

while (devid 

if (mDevicesById[devid].device == NULL) {

break;

}

devid++;

}

......

mDevicesById[devid].seq = (mDevicesById[devid].seq+(1<

if (mDevicesById[devid].seq == 0) {

mDevicesById[devid].seq = 1<

}

new_mFDs = (pollfd*)realloc(mFDs, sizeof(mFDs[0]) * (mFDCount + 1));

new_devices = (device_t**)realloc(mDevices, sizeof(mDevices[0]) * (mFDCount + 1));

if (new_mFDs == NULL|| new_devices ==NULL) {

LOGE("out of memory");

return-1;

}

mFDs = new_mFDs;

mDevices = new_devices;

......

device_t* device = new device_t(devid|mDevicesById[devid].seq, deviceName, name);

if (device == NULL) {

LOGE("out of memory");

return-1;

}

device->fd = fd;

mFDs[mFDCount].fd = fd;

mFDs[mFDCount].events = POLLIN;

mFDs[mFDCount].revents = 0;

// Figure outthe kindsofevents the device reports.

uint8_t key_bitmask[sizeof_bit_array(KEY_MAX + 1)];

memset(key_bitmask, 0, sizeof(key_bitmask));

LOGV("Getting keys...");

if (ioctl(fd, EVIOCGBIT(EV_KEY, sizeof(key_bitmask)), key_bitmask) >= 0) {

// See if this isa keyboard.Ignoreeverythinginthe button rangeexceptfor

// gamepads which are also considered keyboards.

if (containsNonZeroByte(key_bitmask, 0, sizeof_bit_array(BTN_MISC))

|| containsNonZeroByte(key_bitmask, sizeof_bit_array(BTN_GAMEPAD),

sizeof_bit_array(BTN_DIGI))

|| containsNonZeroByte(key_bitmask, sizeof_bit_array(KEY_OK),

sizeof_bit_array(KEY_MAX + 1))) {

device->classes |= INPUT_DEVICE_CLASS_KEYBOARD;

device->keyBitmask = new uint8_t[sizeof(key_bitmask)];

if (device->keyBitmask != NULL) {

memcpy(device->keyBitmask, key_bitmask, sizeof(key_bitmask));

} else{

deletedevice;

LOGE("out of memory allocating key bitmask");

return-1;

}

}

}

......

if ((device->classes & INPUT_DEVICE_CLASS_KEYBOARD) != 0) {

chartmpfn[sizeof(name)];

charkeylayoutFilename[300];

// a more descriptive name

device->name=name;

// replaceallthe spaceswithunderscores

strcpy(tmpfn, name);

for(char*p = strchr(tmpfn,' '); p && *p; p = strchr(tmpfn,' '))

*p = '_';

// find the .kl file we need forthis device

const char* root = getenv("ANDROID_ROOT");

snprintf(keylayoutFilename, sizeof(keylayoutFilename),

"%s/usr/keylayout/%s.kl", root, tmpfn);

bool defaultKeymap = false;

if (access(keylayoutFilename, R_OK)) {

snprintf(keylayoutFilename, sizeof(keylayoutFilename),

"%s/usr/keylayout/%s", root,"qwerty.kl");

defaultKeymap = true;

}

status_t status = device->layoutMap->load(keylayoutFilename);

if (status) {

LOGE("Error %d loading key layout.", status);

}

// tell the world about the devname (the descriptive name)

if (!mHaveFirstKeyboard && !defaultKeymap && strstr(name,"-keypad")) {

// the built-inkeyboard has a well-known device IDof0,

// this device better notgo away.

mHaveFirstKeyboard = true;

mFirstKeyboardId = device->id;

property_set("hw.keyboards.0.devname",name);

} else{

// ensure mFirstKeyboardId issetto-something-.

if (mFirstKeyboardId == 0) {

mFirstKeyboardId = device->id;

}

}

charpropName[100];

sprintf(propName, "hw.keyboards.%u.devname", device->id);

property_set(propName, name);

// 'Q'keysupport = cheap testofwhether thisisan alpha-capable kbd

if (hasKeycodeLocked(device, AKEYCODE_Q)) {

device->classes |= INPUT_DEVICE_CLASS_ALPHAKEY;

}

// See if this device has a DPAD.

if (hasKeycodeLocked(device, AKEYCODE_DPAD_UP) &&

hasKeycodeLocked(device, AKEYCODE_DPAD_DOWN) &&

hasKeycodeLocked(device, AKEYCODE_DPAD_LEFT) &&

hasKeycodeLocked(device, AKEYCODE_DPAD_RIGHT) &&

hasKeycodeLocked(device, AKEYCODE_DPAD_CENTER)) {

device->classes |= INPUT_DEVICE_CLASS_DPAD;

}

// See if this device has a gamepad.

for(size_t i = 0; i 

if (hasKeycodeLocked(device, GAMEPAD_KEYCODES[i])) {

device->classes |= INPUT_DEVICE_CLASS_GAMEPAD;

break;

}

}

LOGI("New keyboard: device->id=0x%x devname='%s' propName='%s' keylayout='%s'\n",

device->id, name, propName, keylayoutFilename);

}

......

mDevicesById[devid].device = device;

device->next= mOpeningDevices;

mOpeningDevices = device;

mDevices[mFDCount] = device;

mFDCount++;

return0;

}

 类似资料: