Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6ec6f79e authored by Jeff Brown's avatar Jeff Brown
Browse files

Support loading keyboard layout overlays from resources.

Added the concept of a keyboard layout overlay, which is
a key character map file that has "type OVERLAY".

Added support for loading keyboard layout overlays from
resources dynamically.  The layouts are reloaded whenever they
are changed in the Settings application or an application
is installed.  This is somewhat more aggressive than necessary
so we might want to optimize it later.

Before system-ready, the input system uses just the generic
keyboard layouts that are included on the device system image.
After system-ready, it considers the user's selected keyboard
layout overlay and attempts to load it as necessary.  We need to
wait until system-ready before doing this because we need to
be in a state where it is safe to start applications or access
their resources.

Bug: 6110399
Change-Id: Iae0886d3356649b0d2440aa00910a888cedd8323
parent a3bc5658
Loading
Loading
Loading
Loading
+27 −2
Original line number Diff line number Diff line
@@ -49,6 +49,17 @@ public:
        KEYBOARD_TYPE_ALPHA = 3,
        KEYBOARD_TYPE_FULL = 4,
        KEYBOARD_TYPE_SPECIAL_FUNCTION = 5,
        KEYBOARD_TYPE_OVERLAY = 6,
    };

    enum Format {
        // Base keyboard layout, may contain device-specific options, such as "type" declaration.
        FORMAT_BASE = 0,
        // Overlay keyboard layout, more restrictive, may be published by applications,
        // cannot override device-specific options.
        FORMAT_OVERLAY = 1,
        // Either base or overlay layout ok.
        FORMAT_ANY = 2,
    };

    // Substitute key code and meta state for fallback action.
@@ -58,7 +69,15 @@ public:
    };

    /* Loads a key character map from a file. */
    static status_t load(const String8& filename, sp<KeyCharacterMap>* outMap);
    static status_t load(const String8& filename, Format format, sp<KeyCharacterMap>* outMap);

    /* Loads a key character map from its string contents. */
    static status_t loadContents(const String8& filename,
            const char* contents, Format format, sp<KeyCharacterMap>* outMap);

    /* Combines a base key character map and an overlay. */
    static sp<KeyCharacterMap> combine(const sp<KeyCharacterMap>& base,
            const sp<KeyCharacterMap>& overlay);

    /* Returns an empty key character map. */
    static sp<KeyCharacterMap> empty();
@@ -115,6 +134,7 @@ protected:
private:
    struct Behavior {
        Behavior();
        Behavior(const Behavior& other);

        /* The next behavior in the list, or NULL if none. */
        Behavior* next;
@@ -131,6 +151,7 @@ private:

    struct Key {
        Key();
        Key(const Key& other);
        ~Key();

        /* The single character label printed on the key, or 0 if none. */
@@ -166,11 +187,12 @@ private:

        KeyCharacterMap* mMap;
        Tokenizer* mTokenizer;
        Format mFormat;
        State mState;
        int32_t mKeyCode;

    public:
        Parser(KeyCharacterMap* map, Tokenizer* tokenizer);
        Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format);
        ~Parser();
        status_t parse();

@@ -188,6 +210,7 @@ private:
    int mType;

    KeyCharacterMap();
    KeyCharacterMap(const KeyCharacterMap& other);

    bool getKey(int32_t keyCode, const Key** outKey) const;
    bool getKeyBehavior(int32_t keyCode, int32_t metaState,
@@ -195,6 +218,8 @@ private:

    bool findKey(char16_t ch, int32_t* outKeyCode, int32_t* outMetaState) const;

    static status_t load(Tokenizer* tokenizer, Format format, sp<KeyCharacterMap>* outMap);

    static void addKey(Vector<KeyEvent>& outEvents,
            int32_t deviceId, int32_t keyCode, int32_t metaState, bool down, nsecs_t time);
    static void addMetaKeys(Vector<KeyEvent>& outEvents,
+104 −23
Original line number Diff line number Diff line
@@ -89,6 +89,13 @@ KeyCharacterMap::KeyCharacterMap() :
    mType(KEYBOARD_TYPE_UNKNOWN) {
}

KeyCharacterMap::KeyCharacterMap(const KeyCharacterMap& other) :
    RefBase(), mType(other.mType) {
    for (size_t i = 0; i < other.mKeys.size(); i++) {
        mKeys.add(other.mKeys.keyAt(i), new Key(*other.mKeys.valueAt(i)));
    }
}

KeyCharacterMap::~KeyCharacterMap() {
    for (size_t i = 0; i < mKeys.size(); i++) {
        Key* key = mKeys.editValueAt(i);
@@ -96,7 +103,8 @@ KeyCharacterMap::~KeyCharacterMap() {
    }
}

status_t KeyCharacterMap::load(const String8& filename, sp<KeyCharacterMap>* outMap) {
status_t KeyCharacterMap::load(const String8& filename,
        Format format, sp<KeyCharacterMap>* outMap) {
    outMap->clear();

    Tokenizer* tokenizer;
@@ -104,6 +112,30 @@ status_t KeyCharacterMap::load(const String8& filename, sp<KeyCharacterMap>* out
    if (status) {
        ALOGE("Error %d opening key character map file %s.", status, filename.string());
    } else {
        status = load(tokenizer, format, outMap);
        delete tokenizer;
    }
    return status;
}

status_t KeyCharacterMap::loadContents(const String8& filename, const char* contents,
        Format format, sp<KeyCharacterMap>* outMap) {
    outMap->clear();

    Tokenizer* tokenizer;
    status_t status = Tokenizer::fromContents(filename, contents, &tokenizer);
    if (status) {
        ALOGE("Error %d opening key character map.", status);
    } else {
        status = load(tokenizer, format, outMap);
        delete tokenizer;
    }
    return status;
}

status_t KeyCharacterMap::load(Tokenizer* tokenizer,
        Format format, sp<KeyCharacterMap>* outMap) {
    status_t status = OK;
    sp<KeyCharacterMap> map = new KeyCharacterMap();
    if (!map.get()) {
        ALOGE("Error allocating key character map.");
@@ -112,7 +144,7 @@ status_t KeyCharacterMap::load(const String8& filename, sp<KeyCharacterMap>* out
#if DEBUG_PARSER_PERFORMANCE
        nsecs_t startTime = systemTime(SYSTEM_TIME_MONOTONIC);
#endif
            Parser parser(map.get(), tokenizer);
        Parser parser(map.get(), tokenizer, format);
        status = parser.parse();
#if DEBUG_PARSER_PERFORMANCE
        nsecs_t elapsedTime = systemTime(SYSTEM_TIME_MONOTONIC) - startTime;
@@ -124,11 +156,33 @@ status_t KeyCharacterMap::load(const String8& filename, sp<KeyCharacterMap>* out
            *outMap = map;
        }
    }
        delete tokenizer;
    }
    return status;
}

sp<KeyCharacterMap> KeyCharacterMap::combine(const sp<KeyCharacterMap>& base,
        const sp<KeyCharacterMap>& overlay) {
    if (overlay == NULL) {
        return base;
    }
    if (base == NULL) {
        return overlay;
    }

    sp<KeyCharacterMap> map = new KeyCharacterMap(*base.get());
    for (size_t i = 0; i < overlay->mKeys.size(); i++) {
        int32_t keyCode = overlay->mKeys.keyAt(i);
        Key* key = overlay->mKeys.valueAt(i);
        ssize_t oldIndex = map->mKeys.indexOfKey(keyCode);
        if (oldIndex >= 0) {
            delete map->mKeys.valueAt(oldIndex);
            map->mKeys.editValueAt(oldIndex) = new Key(*key);
        } else {
            map->mKeys.add(keyCode, new Key(*key));
        }
    }
    return map;
}

sp<KeyCharacterMap> KeyCharacterMap::empty() {
    return sEmpty;
}
@@ -508,6 +562,11 @@ KeyCharacterMap::Key::Key() :
        label(0), number(0), firstBehavior(NULL) {
}

KeyCharacterMap::Key::Key(const Key& other) :
        label(other.label), number(other.number),
        firstBehavior(other.firstBehavior ? new Behavior(*other.firstBehavior) : NULL) {
}

KeyCharacterMap::Key::~Key() {
    Behavior* behavior = firstBehavior;
    while (behavior) {
@@ -524,11 +583,17 @@ KeyCharacterMap::Behavior::Behavior() :
        next(NULL), metaState(0), character(0), fallbackKeyCode(0) {
}

KeyCharacterMap::Behavior::Behavior(const Behavior& other) :
        next(other.next ? new Behavior(*other.next) : NULL),
        metaState(other.metaState), character(other.character),
        fallbackKeyCode(other.fallbackKeyCode) {
}


// --- KeyCharacterMap::Parser ---

KeyCharacterMap::Parser::Parser(KeyCharacterMap* map, Tokenizer* tokenizer) :
        mMap(map), mTokenizer(tokenizer), mState(STATE_TOP) {
KeyCharacterMap::Parser::Parser(KeyCharacterMap* map, Tokenizer* tokenizer, Format format) :
        mMap(map), mTokenizer(tokenizer), mFormat(format), mState(STATE_TOP) {
}

KeyCharacterMap::Parser::~Parser() {
@@ -588,11 +653,25 @@ status_t KeyCharacterMap::Parser::parse() {
        return BAD_VALUE;
    }

    if (mFormat == FORMAT_BASE) {
        if (mMap->mType == KEYBOARD_TYPE_UNKNOWN) {
        ALOGE("%s: Missing required keyboard 'type' declaration.",
            ALOGE("%s: Base keyboard layout missing required keyboard 'type' declaration.",
                    mTokenizer->getLocation().string());
            return BAD_VALUE;
        }
        if (mMap->mType == KEYBOARD_TYPE_OVERLAY) {
            ALOGE("%s: Base keyboard layout must specify a keyboard 'type' other than 'OVERLAY'.",
                    mTokenizer->getLocation().string());
            return BAD_VALUE;
        }
    } else if (mFormat == FORMAT_OVERLAY) {
        if (mMap->mType != KEYBOARD_TYPE_OVERLAY) {
            ALOGE("%s: Overlay keyboard layout missing required keyboard "
                    "'type OVERLAY' declaration.",
                    mTokenizer->getLocation().string());
            return BAD_VALUE;
        }
    }

    return NO_ERROR;
}
@@ -616,6 +695,8 @@ status_t KeyCharacterMap::Parser::parseType() {
        type = KEYBOARD_TYPE_FULL;
    } else if (typeToken == "SPECIAL_FUNCTION") {
        type = KEYBOARD_TYPE_SPECIAL_FUNCTION;
    } else if (typeToken == "OVERLAY") {
        type = KEYBOARD_TYPE_OVERLAY;
    } else {
        ALOGE("%s: Expected keyboard type label, got '%s'.", mTokenizer->getLocation().string(),
                typeToken.string());
+2 −1
Original line number Diff line number Diff line
@@ -129,7 +129,8 @@ status_t KeyMap::loadKeyCharacterMap(const InputDeviceIdentifier& deviceIdentifi
        return NAME_NOT_FOUND;
    }

    status_t status = KeyCharacterMap::load(path, &keyCharacterMap);
    status_t status = KeyCharacterMap::load(path,
            KeyCharacterMap::FORMAT_BASE, &keyCharacterMap);
    if (status) {
        return status;
    }
+28 −2
Original line number Diff line number Diff line
# Copyright (C) 2012 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.

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

@@ -12,5 +26,17 @@ LOCAL_CERTIFICATE := platform

include $(BUILD_PACKAGE)

########################
include $(call all-makefiles-under,$(LOCAL_PATH))
# Validate all key maps.
include $(CLEAR_VARS)

validatekeymaps := $(HOST_OUT_EXECUTABLES)/validatekeymaps$(HOST_EXECUTABLE_SUFFIX)
files := frameworks/base/packages/InputDevices/res/raw/*.kcm

LOCAL_MODULE := validate_input_devices_keymaps
LOCAL_MODULE_TAGS := optional
LOCAL_REQUIRED_MODULES := validatekeymaps

validate_input_devices_keymaps: $(files)
	$(hide) $(validatekeymaps) $(files)

include $(BUILD_PHONY_PACKAGE)
+6 −1
Original line number Diff line number Diff line
@@ -12,4 +12,9 @@
# See the License for the specific language governing permissions and
# limitations under the License.

# PLACEHOLDER CONTENT #
#
# English (US) keyboard layout.
# Assumes that the base keyboard layout is already English (US).
#

type OVERLAY
Loading