Eliminate hw.keyboards system properties.

Stop using system properties to publish information about
the key character map path.  Instead, we can retrieve it
on demand by asking the window manager.

It was possible to exhaust the supply of system properties
when repeatedly adding and removing input devices.

Bug: 5532806
Change-Id: Idd361a24ad7db2edc185c8546db7fb05f9c28669
This commit is contained in:
Jeff Brown
2011-11-15 17:48:10 -08:00
parent 9058435dc1
commit 1e08fe90df
13 changed files with 67 additions and 104 deletions

View File

@@ -19,7 +19,6 @@ package android.view;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.RemoteException;
import android.os.ServiceManager;
import java.util.ArrayList;
import java.util.List;
@@ -44,6 +43,7 @@ public final class InputDevice implements Parcelable {
private String mName;
private int mSources;
private int mKeyboardType;
private String mKeyCharacterMapFile;
private final ArrayList<MotionRange> mMotionRanges = new ArrayList<MotionRange>();
@@ -360,6 +360,10 @@ public final class InputDevice implements Parcelable {
return KeyCharacterMap.load(mId);
}
String getKeyCharacterMapFile() {
return mKeyCharacterMapFile;
}
/**
* Gets information about the range of values for a particular {@link MotionEvent} axis.
* If the device supports multiple sources, the same axis may have different meanings
@@ -532,6 +536,7 @@ public final class InputDevice implements Parcelable {
mName = in.readString();
mSources = in.readInt();
mKeyboardType = in.readInt();
mKeyCharacterMapFile = in.readString();
for (;;) {
int axis = in.readInt();
@@ -549,6 +554,7 @@ public final class InputDevice implements Parcelable {
out.writeString(mName);
out.writeInt(mSources);
out.writeInt(mKeyboardType);
out.writeString(mKeyCharacterMapFile);
final int numRanges = mMotionRanges.size();
for (int i = 0; i < numRanges; i++) {
@@ -587,6 +593,8 @@ public final class InputDevice implements Parcelable {
}
description.append("\n");
description.append(" Key Character Map: ").append(mKeyCharacterMapFile).append("\n");
description.append(" Sources: 0x").append(Integer.toHexString(mSources)).append(" (");
appendSourceDescriptionIfApplicable(description, SOURCE_KEYBOARD, "keyboard");
appendSourceDescriptionIfApplicable(description, SOURCE_DPAD, "dpad");

View File

@@ -20,7 +20,6 @@ import android.text.method.MetaKeyKeyListener;
import android.util.AndroidRuntimeException;
import android.util.SparseIntArray;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.SparseArray;
import java.lang.Character;
@@ -140,7 +139,7 @@ public class KeyCharacterMap {
private final int mDeviceId;
private int mPtr;
private static native int nativeLoad(int id);
private static native int nativeLoad(String file);
private static native void nativeDispose(int ptr);
private static native char nativeGetCharacter(int ptr, int keyCode, int metaState);
@@ -178,7 +177,17 @@ public class KeyCharacterMap {
synchronized (sInstances) {
KeyCharacterMap map = sInstances.get(deviceId);
if (map == null) {
int ptr = nativeLoad(deviceId); // might throw
String kcm = null;
if (deviceId != VIRTUAL_KEYBOARD) {
InputDevice device = InputDevice.getDevice(deviceId);
if (device != null) {
kcm = device.getKeyCharacterMapFile();
}
}
if (kcm == null || kcm.length() == 0) {
kcm = "/system/usr/keychars/Virtual.kcm";
}
int ptr = nativeLoad(kcm); // might throw
map = new KeyCharacterMap(deviceId, ptr);
sInstances.put(deviceId, map);
}

View File

@@ -35,18 +35,25 @@ static struct {
} gFallbackActionClassInfo;
static jint nativeLoad(JNIEnv *env, jobject clazz, jint deviceId) {
static jint nativeLoad(JNIEnv *env, jobject clazz, jstring fileStr) {
const char* file = env->GetStringUTFChars(fileStr, NULL);
KeyCharacterMap* map;
status_t status = KeyCharacterMap::loadByDeviceId(deviceId, &map);
status_t status = KeyCharacterMap::load(String8(file), &map);
jint result;
if (status) {
String8 msg;
msg.appendFormat("Could not load key character map for device %d due to error %d. "
"Refer to the log for details.", deviceId, status);
msg.appendFormat("Could not load key character map '%s' due to error %d. "
"Refer to the log for details.", file, status);
jniThrowException(env, "android/view/KeyCharacterMap$KeyCharacterMapUnavailableException",
msg.string());
return 0;
result = 0;
} else {
result = reinterpret_cast<jint>(map);
}
return reinterpret_cast<jint>(map);
env->ReleaseStringUTFChars(fileStr, file);
return result;
}
static void nativeDispose(JNIEnv *env, jobject clazz, jint ptr) {
@@ -141,7 +148,7 @@ static jobjectArray nativeGetEvents(JNIEnv *env, jobject clazz, jint ptr, jint d
static JNINativeMethod g_methods[] = {
/* name, signature, funcPtr */
{ "nativeLoad", "(I)I",
{ "nativeLoad", "(Ljava/lang/String;)I",
(void*)nativeLoad },
{ "nativeDispose", "(I)V",
(void*)nativeDispose },

View File

@@ -826,6 +826,9 @@ public:
inline void setKeyboardType(int32_t keyboardType) { mKeyboardType = keyboardType; }
inline int32_t getKeyboardType() const { return mKeyboardType; }
inline void setKeyCharacterMapFile(const String8& value) { mKeyCharacterMapFile = value; }
inline const String8& getKeyCharacterMapFile() const { return mKeyCharacterMapFile; }
inline const Vector<MotionRange>& getMotionRanges() const {
return mMotionRanges;
}
@@ -835,6 +838,7 @@ private:
String8 mName;
uint32_t mSources;
int32_t mKeyboardType;
String8 mKeyCharacterMapFile;
Vector<MotionRange> mMotionRanges;
};

View File

@@ -53,7 +53,6 @@ public:
~KeyCharacterMap();
static status_t load(const String8& filename, KeyCharacterMap** outMap);
static status_t loadByDeviceId(int32_t deviceId, KeyCharacterMap** outMap);
/* Gets the keyboard type. */
int32_t getKeyboardType() const;

View File

@@ -80,24 +80,6 @@ private:
extern bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
const PropertyMap* deviceConfiguration, const KeyMap* keyMap);
/**
* Sets keyboard system properties.
*/
extern void setKeyboardProperties(int32_t deviceId, const InputDeviceIdentifier& deviceIdentifier,
const String8& keyLayoutFile, const String8& keyCharacterMapFile);
/**
* Clears keyboard system properties.
*/
extern void clearKeyboardProperties(int32_t deviceId);
/**
* Gets the key character map filename for a device using inspecting system properties
* and then falling back on a default key character map if necessary.
* Returns a NAME_NOT_FOUND if none found.
*/
extern status_t getKeyCharacterMapFile(int32_t deviceId, String8& outKeyCharacterMapFile);
/**
* Gets a key code by its short form label, eg. "HOME".
* Returns 0 if unknown.

View File

@@ -124,17 +124,6 @@ status_t KeyCharacterMap::load(const String8& filename, KeyCharacterMap** outMap
return status;
}
status_t KeyCharacterMap::loadByDeviceId(int32_t deviceId, KeyCharacterMap** outMap) {
*outMap = NULL;
String8 filename;
status_t result = getKeyCharacterMapFile(deviceId, filename);
if (!result) {
result = load(filename, outMap);
}
return result;
}
int32_t KeyCharacterMap::getKeyboardType() const {
return mType;
}

View File

@@ -173,50 +173,6 @@ bool isEligibleBuiltInKeyboard(const InputDeviceIdentifier& deviceIdentifier,
return strstr(deviceIdentifier.name.string(), "-keypad");
}
void setKeyboardProperties(int32_t deviceId,
const InputDeviceIdentifier& deviceIdentifier,
const String8& keyLayoutFile, const String8& keyCharacterMapFile) {
char propName[PROPERTY_KEY_MAX];
snprintf(propName, sizeof(propName), "hw.keyboards.%u.devname", deviceId);
property_set(propName, deviceIdentifier.name.string());
snprintf(propName, sizeof(propName), "hw.keyboards.%u.klfile", deviceId);
property_set(propName, keyLayoutFile.string());
snprintf(propName, sizeof(propName), "hw.keyboards.%u.kcmfile", deviceId);
property_set(propName, keyCharacterMapFile.string());
}
void clearKeyboardProperties(int32_t deviceId) {
char propName[PROPERTY_KEY_MAX];
snprintf(propName, sizeof(propName), "hw.keyboards.%u.devname", deviceId);
property_set(propName, "");
snprintf(propName, sizeof(propName), "hw.keyboards.%u.klfile", deviceId);
property_set(propName, "");
snprintf(propName, sizeof(propName), "hw.keyboards.%u.kcmfile", deviceId);
property_set(propName, "");
}
status_t getKeyCharacterMapFile(int32_t deviceId, String8& outKeyCharacterMapFile) {
if (deviceId != DEVICE_ID_VIRTUAL_KEYBOARD) {
char propName[PROPERTY_KEY_MAX];
char fn[PROPERTY_VALUE_MAX];
snprintf(propName, sizeof(propName), "hw.keyboards.%u.kcmfile", deviceId);
if (property_get(propName, fn, "") > 0) {
outKeyCharacterMapFile.setTo(fn);
return OK;
}
}
// Default to Virtual since the keyboard does not appear to be installed.
outKeyCharacterMapFile.setTo(getInputDeviceConfigurationFilePathByName(String8("Virtual"),
INPUT_DEVICE_CONFIGURATION_FILE_TYPE_KEY_CHARACTER_MAP));
if (!outKeyCharacterMapFile.isEmpty()) {
return OK;
}
LOGE("Can't find any key character map files including the Virtual key map!");
return NAME_NOT_FOUND;
}
static int lookupValueByLabel(const char* literal, const KeycodeLabel *list) {
while (list->literal) {
if (strcmp(literal, list->literal) == 0) {

View File

@@ -507,6 +507,15 @@ void EventHub::getVirtualKeyDefinitions(int32_t deviceId,
}
}
String8 EventHub::getKeyCharacterMapFile(int32_t deviceId) const {
AutoMutex _l(mLock);
Device* device = getDeviceLocked(deviceId);
if (device) {
return device->keyMap.keyCharacterMapFile;
}
return String8();
}
EventHub::Device* EventHub::getDeviceLocked(int32_t deviceId) const {
if (deviceId == 0) {
deviceId = mBuiltInKeyboardId;
@@ -1013,16 +1022,12 @@ status_t EventHub::openDeviceLocked(const char *devicePath) {
// Configure the keyboard, gamepad or virtual keyboard.
if (device->classes & INPUT_DEVICE_CLASS_KEYBOARD) {
// Set system properties for the keyboard.
setKeyboardPropertiesLocked(device, false);
// Register the keyboard as a built-in keyboard if it is eligible.
if (!keyMapStatus
&& mBuiltInKeyboardId == -1
&& isEligibleBuiltInKeyboard(device->identifier,
device->configuration, &device->keyMap)) {
mBuiltInKeyboardId = device->id;
setKeyboardPropertiesLocked(device, true);
}
// 'Q' key support = cheap test of whether this is an alpha-capable kbd
@@ -1120,17 +1125,6 @@ status_t EventHub::loadKeyMapLocked(Device* device) {
return device->keyMap.load(device->identifier, device->configuration);
}
void EventHub::setKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
int32_t id = builtInKeyboard ? 0 : device->id;
android::setKeyboardProperties(id, device->identifier,
device->keyMap.keyLayoutFile, device->keyMap.keyCharacterMapFile);
}
void EventHub::clearKeyboardPropertiesLocked(Device* device, bool builtInKeyboard) {
int32_t id = builtInKeyboard ? 0 : device->id;
android::clearKeyboardProperties(id);
}
bool EventHub::isExternalDeviceLocked(Device* device) {
if (device->configuration) {
bool value;
@@ -1184,9 +1178,7 @@ void EventHub::closeDeviceLocked(Device* device) {
LOGW("built-in keyboard device %s (id=%d) is closing! the apps will not like this",
device->path.string(), mBuiltInKeyboardId);
mBuiltInKeyboardId = -1;
clearKeyboardPropertiesLocked(device, true);
}
clearKeyboardPropertiesLocked(device, false);
if (epoll_ctl(mEpollFd, EPOLL_CTL_DEL, device->fd, NULL)) {
LOGW("Could not remove device fd from epoll instance. errno=%d", errno);

View File

@@ -208,6 +208,8 @@ public:
virtual void getVirtualKeyDefinitions(int32_t deviceId,
Vector<VirtualKeyDefinition>& outVirtualKeys) const = 0;
virtual String8 getKeyCharacterMapFile(int32_t deviceId) const = 0;
/* Requests the EventHub to reopen all input devices on the next call to getEvents(). */
virtual void requestReopenDevices() = 0;
@@ -264,6 +266,8 @@ public:
virtual void getVirtualKeyDefinitions(int32_t deviceId,
Vector<VirtualKeyDefinition>& outVirtualKeys) const;
virtual String8 getKeyCharacterMapFile(int32_t deviceId) const;
virtual void requestReopenDevices();
virtual void wake();
@@ -321,8 +325,6 @@ private:
void loadConfigurationLocked(Device* device);
status_t loadVirtualKeyMapLocked(Device* device);
status_t loadKeyMapLocked(Device* device);
void setKeyboardPropertiesLocked(Device* device, bool builtInKeyboard);
void clearKeyboardPropertiesLocked(Device* device, bool builtInKeyboard);
bool isExternalDeviceLocked(Device* device);

View File

@@ -1775,6 +1775,7 @@ void KeyboardInputMapper::populateDeviceInfo(InputDeviceInfo* info) {
InputMapper::populateDeviceInfo(info);
info->setKeyboardType(mKeyboardType);
info->setKeyCharacterMapFile(getEventHub()->getKeyCharacterMapFile(getDeviceId()));
}
void KeyboardInputMapper::dump(String8& dump) {

View File

@@ -609,6 +609,10 @@ private:
}
}
virtual String8 getKeyCharacterMapFile(int32_t deviceId) const {
return String8();
}
virtual bool isExternal(int32_t deviceId) const {
return false;
}

View File

@@ -98,6 +98,7 @@ static struct {
jfieldID mName;
jfieldID mSources;
jfieldID mKeyboardType;
jfieldID mKeyCharacterMapFile;
} gInputDeviceClassInfo;
static struct {
@@ -1231,10 +1232,16 @@ static jobject android_server_InputManager_nativeGetInputDevice(JNIEnv* env,
return NULL;
}
jstring fileStr = env->NewStringUTF(deviceInfo.getKeyCharacterMapFile());
if (!fileStr) {
return NULL;
}
env->SetIntField(deviceObj, gInputDeviceClassInfo.mId, deviceInfo.getId());
env->SetObjectField(deviceObj, gInputDeviceClassInfo.mName, deviceNameObj);
env->SetIntField(deviceObj, gInputDeviceClassInfo.mSources, deviceInfo.getSources());
env->SetIntField(deviceObj, gInputDeviceClassInfo.mKeyboardType, deviceInfo.getKeyboardType());
env->SetObjectField(deviceObj, gInputDeviceClassInfo.mKeyCharacterMapFile, fileStr);
const Vector<InputDeviceInfo::MotionRange>& ranges = deviceInfo.getMotionRanges();
for (size_t i = 0; i < ranges.size(); i++) {
@@ -1516,6 +1523,9 @@ int register_android_server_InputManager(JNIEnv* env) {
GET_FIELD_ID(gInputDeviceClassInfo.mKeyboardType, gInputDeviceClassInfo.clazz,
"mKeyboardType", "I");
GET_FIELD_ID(gInputDeviceClassInfo.mKeyCharacterMapFile, gInputDeviceClassInfo.clazz,
"mKeyCharacterMapFile", "Ljava/lang/String;");
// Configuration
FIND_CLASS(clazz, "android/content/res/Configuration");