Android TP virtual Key Processing

Source: Internet
Author: User

Android TP virtual Key Processing

Nowadays, more and more Android mobile phones are operated by virtual buttons, but developers may be concerned about how Android handles virtual keys. People familiar with Linux may say it's easy to call input_report_key (). Okay, you are not making a mistake, but in Android, Google gives you a better understanding of policies and drivers.

APP ------->
Framework ------->
Kernel ------->
Hardware

The preceding figure shows the entire virtual key of Android.
Because it is a driver, we should start with the driver.
In fact, the processing of the virtual key is not difficult for the driver. With the touch panel driver, you can implement the underlying driver of the virtual key. Have you tried this? You may say, "No, it cannot be implemented here ". Yes, this time is not enough. There are still key steps to perform.

Here, you need to add the following code.

static unsigned int tpd_keycnt = 0;static int tpd_keys[TPD_VIRTUAL_KEY_MAX]={0};static int tpd_keys_dim[TPD_VIRTUAL_KEY_MAX][4];// = {0};static ssize_t cust_virtual_keys_show(struct kobject *kobj,                   struct kobj_attribute *attr, char *buf) {    int i, j;    for(i=0, j=0;i<tpd_keycnt;i++)        j+=sprintf(buf, "%s%s:%d:%d:%d:%d:%d%s",buf,           __stringify(EV_KEY),tpd_keys[i],           tpd_keys_dim[i][0],tpd_keys_dim[i][1],           tpd_keys_dim[i][2],tpd_keys_dim[i][3],           (i==tpd_keycnt-1?"\n":":"));    return j;}static struct kobj_attribute cust_virtual_keys_attr = {    .attr = {        .name = "virtualkeys.cust-tpd",        .mode = S_IRUGO,    },    .show = &cust_virtual_keys_show,};static struct attribute *cust_properties_attrs[] = {    &cust_virtual_keys_attr.attr,    NULL};static struct attribute_group cust_properties_attr_group = {    .attrs = cust_properties_attrs,};struct kobject *properties_kobj;void tpd_button_init(void) {    int ret = 0, i = 0, j=0;    tpd->kpd=input_allocate_device();    /* struct input_dev kpd initialization and registration */    tpd->kpd->name = TPD_DEVICE "-kpd";    set_bit(EV_KEY, tpd->kpd->evbit);    for(i=0;i<tpd_keycnt;i++)        __set_bit(tpd_keys[i], tpd->kpd->keybit);    tpd->kpd->id.bustype = BUS_HOST;    tpd->kpd->id.vendor  = 0x0001;    tpd->kpd->id.product = 0x0001;    tpd->kpd->id.version = 0x0100;    if(input_register_device(tpd->kpd))        TPD_DMESG("input_register_device failed.(kpd)\n");    set_bit(EV_KEY, tpd->dev->evbit);    for(i=0;i<tpd_keycnt;i++)        __set_bit(tpd_keys[i], tpd->dev->keybit);    properties_kobj = kobject_create_and_add("board_properties", NULL);    if(properties_kobj)        ret = sysfs_create_group(properties_kobj,&cust_properties_attr_group);    if(!properties_kobj || ret)    printk("failed to create board_properties\n");}void tpd_button_setting(int keycnt, void *keys, void *keys_dim){tpd_keycnt = keycnt;memcpy(tpd_keys, keys, keycnt*4);memcpy(tpd_keys_dim, keys_dim, keycnt*4*4);}

With the above Code, our virtual key can be used. Here we mainly need to register/sys/board_properties/virtualkeys. Cust-TPD. This is the file node required by the framework. His appearance can make our virtual buttons unblocked.
Of course, here tpd_keys, the array of the defined key and the tpd_keys_dim of the defined area, must be accurately filled. The specific filling rules are as follows:
Each virtual key has six parameters:

0x01: A version code. must always be 0x01. <Linux key code>: the Linux key code of the virtual key. <centerx>: The X pixel coordinate of the center of the virtual key. <centery>: The Y pixel coordinate of the center of the virtual key. <width>: the width of the virtual key in pixels. 

It can be seen that the defined back, menu, home, search and specific regions are also clear.

The following is the processing in the framework. The file is in framework/base/services/Java/COM/Android/Server/inputmanager. java.
Call getvirtualkeydefinitions to obtain the defined virtual buttons.
 

public VirtualKeyDefinition[] getVirtualKeyDefinitions(String deviceName) {            ArrayList<VirtualKeyDefinition> keys = new ArrayList<VirtualKeyDefinition>();                        try {                FileInputStream fis = new FileInputStream(                        "/sys/board_properties/virtualkeys." + deviceName);                InputStreamReader isr = new InputStreamReader(fis);                BufferedReader br = new BufferedReader(isr, 2048);                String str = br.readLine();                if (str != null) {                    String[] it = str.split(":");                    if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "***** VIRTUAL KEYS: " + it);                    final int N = it.length-6;                    for (int i=0; i<=N; i+=6) {                        if (!"0x01".equals(it[i])) {                            Slog.w(TAG, "Unknown virtual key type at elem #"                                    + i + ": " + it[i] + " for device " + deviceName);                            continue;                        }                        try {                            VirtualKeyDefinition key = new VirtualKeyDefinition();                            key.scanCode = Integer.parseInt(it[i+1]);                            key.centerX = Integer.parseInt(it[i+2]);                            key.centerY = Integer.parseInt(it[i+3]);                            key.width = Integer.parseInt(it[i+4]);                            key.height = Integer.parseInt(it[i+5]);                            if (DEBUG_VIRTUAL_KEYS) Slog.v(TAG, "Virtual key "                                    + key.scanCode + ": center=" + key.centerX + ","                                    + key.centerY + " size=" + key.width + "x"                                    + key.height);                            keys.add(key);                        } catch (NumberFormatException e) {                            Slog.w(TAG, "Bad number in virtual key definition at region "                                    + i + " in: " + str + " for device " + deviceName, e);                        }                    }                }                br.close();            } catch (FileNotFoundException e) {                Slog.i(TAG, "No virtual keys found for device " + deviceName + ".");            } catch (IOException e) {                Slog.w(TAG, "Error reading virtual keys for device " + deviceName + ".", e);            }                        return keys.toArray(new VirtualKeyDefinition[keys.size()]);        }

In fact, the call to this function is actually called through JNI com_android_server_inputmanager.cpp and inputreader. cpp.
Finally, the key event is reported to the app through yykey () for processing.

You also need to configure:
Key layout file:/system/usr/keylayout/touchyfeely. KL.

Key 158 back
Key 139 menu
Key 102 home
Key 217 search

Key Character Map File:/system/usr/keychars/touchyfeely. KCM.

Type special_function

In fact, this example gives me the biggest feeling that I have a better understanding of what policies and mechanisms are. Reading the source code can make some changes to your own ideas.

Have fun!

Additional information:

Http://source.android.com/tech/input/touch-devices.html#virtual-key-map-files

Http://source.android.com/tech/input/validate-keymaps.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.