Android驱动之虚拟按键

来源:互联网 发布:淘宝代销什么产品好 编辑:程序博客网 时间:2024/05/22 06:22

1 背景

nexus one工业设计简洁,类似于iphone只有一个按键的设计,只有中间的一个轨迹球。但是android标准键盘是有 HOME,MENU,BACK,SEARCH等,但是同时要保持工业设计。nexus one是这样解决问题的,显示屏是800X480,但是在电容触摸屏是8xx*480的就是比800要大的地方就变成了虚拟按键,模拟了android标准按键。

 

2 方案

要实现,虚拟按键,在android里面是靠两层协助实现,底层要把虚拟按键在比显示屏多出的地方规定好虚拟按键的位置大小以及键值等,给上层一文件接口。上层java层启动一个服务来读取这一区域的按键响应,这样就是大体的架构。具体实现如下:

 

2.1 底层虚拟按键功能实现方案 简而言之,就是在内核中把虚拟按键的所有信息给上层给出,用什么方式?就是用sys文件系统的方式,sys文件系统的路径是约定好的所以代码如下实现。给出信息的协议格式是一段连续的字符串,每个按键有六项分别用冒号分割,按键按键之间也是用冒号分割,六项按顺序分别是: 键类型:键值:按键区域中心x坐标:按键区域中心y坐标:按键区域宽:按键区域高 arch/arm/mach-msm/board-mahimahi.c  

 

 

[c] view plaincopy
  1. static ssize_t mahimahi_virtual_keys_show(struct kobject *kobj,  
  2.                    struct kobj_attribute *attr, char *buf)  
  3. {  
  4.     if (system_rev > 2) {  
  5.         /* center: x: back: 55, menu: 172, home: 298, search 412, y: 835 */  
  6.         return sprintf(buf,  
  7.             __stringify(EV_KEY) ":" __stringify(KEY_BACK)  ":55:835:90:55"  
  8.            ":" __stringify(EV_KEY) ":" __stringify(KEY_MENU)   ":172:835:125:55"  
  9.            ":" __stringify(EV_KEY) ":" __stringify(KEY_HOME)   ":298:835:115:55"  
  10.            ":" __stringify(EV_KEY) ":" __stringify(KEY_SEARCH) ":412:835:95:55"  
  11.            "/n");  
  12.     } else {  
  13.         /* center: x: home: 55, menu: 185, back: 305, search 425, y: 835 */  
  14.         return sprintf(buf,  
  15.             __stringify(EV_KEY) ":" __stringify(KEY_HOME)  ":55:835:70:55"  
  16.            ":" __stringify(EV_KEY) ":" __stringify(KEY_MENU)   ":185:835:100:55"  
  17.            ":" __stringify(EV_KEY) ":" __stringify(KEY_BACK)   ":305:835:70:55"  
  18.            ":" __stringify(EV_KEY) ":" __stringify(KEY_SEARCH) ":425:835:70:55"  
  19.            "/n");  
  20.     }  
  21. }  
  22.    
  23. static struct kobj_attribute mahimahi_virtual_keys_attr = {  
  24.     .attr = {  
  25.         .name = "virtualkeys.synaptics-rmi-touchscreen",  
  26.         .mode = S_IRUGO,  
  27.     },  
  28.     .show = &mahimahi_virtual_keys_show,  
  29. };  
  30.    
  31. static struct attribute *mahimahi_properties_attrs[] = {  
  32.     &mahimahi_virtual_keys_attr.attr,  
  33.     NULL  
  34. };  
  35.    
  36. static struct attribute_group mahimahi_properties_attr_group = {  
  37.     .attrs = mahimahi_properties_attrs,  
  38. };  
  39.    
  40. struct kobject *properties_kobj;  
  41.    
  42. properties_kobj = kobject_create_and_add("board_properties", NULL);  
  43. if (properties_kobj)  
  44.     ret = sysfs_create_group(properties_kobj,  
  45.                      &mahimahi_properties_attr_group);  
  46. if (!properties_kobj || ret)  
  47.     pr_err("failed to create board_properties/n");  

 

JAVA上层方案 Java层主要是读取按键信息,然后经过一定的算法,来识别虚拟按键,基本不需要修改,但最好还是熟悉java层的架构这样出问题的时候利于定位 frameworks/base/services/java/com/android/server/KeyInputQueue.java  

 

   /*这是虚拟按键的类里面包括了VirtualKey所用到的成员变量和按键定位方法*/

[java] view plaincopy
  1.     static class VirtualKey {  
  2.         int scancode;  
  3.         int centerx;  
  4.         int centery;  
  5.         int width;  
  6.         int height;  
  7.    
  8.         int hitLeft;  
  9.         int hitTop;  
  10.         int hitRight;  
  11.         int hitBottom;  
  12.    
  13.         InputDevice lastDevice;  
  14.         int lastKeycode;  
  15.    
  16.         boolean checkHit(int x, int y) {  
  17.             return (x >= hitLeft && x <= hitRight                     && y >= hitTop && y <= hitBottom);  
  18.         }  
  19.    
  20.         void computeHitRect(InputDevice dev, int dw, int dh) {  
  21.             if (dev == lastDevice) {  
  22.                 return;  
  23.             }  
  24.             if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "computeHitRect for " + scancode  
  25.                     + ": dev=" + dev + " absX=" + dev.absX + " absY=" + dev.absY);  
  26.    
  27.             lastDevice = dev;  
  28.    
  29.             int minx = dev.absX.minValue;  
  30.             int maxx = dev.absX.maxValue;  
  31.    
  32.             int halfw = width/2;  
  33.             int left = centerx - halfw;  
  34.             int right = centerx + halfw;  
  35.             hitLeft = minx + ((left*maxx-minx)/dw);  
  36.             hitRight = minx + ((right*maxx-minx)/dw);  
  37.    
  38.             int miny = dev.absY.minValue;  
  39.             int maxy = dev.absY.maxValue;  
  40.    
  41.             int halfh = height/2;  
  42.             int top = centery - halfh;  
  43.             int bottom = centery + halfh;  
  44.             hitTop = miny + ((top*maxy-miny)/dh);  
  45.             hitBottom = miny + ((bottom*maxy-miny)/dh);  
  46.         }  
  47.     }  
  48. /*以下就是与底层接口的函数,如果这个函数和底层接口正常,基本上虚拟按键就能够ok*/  
  49.     private void readVirtualKeys(String deviceName) {  
  50.         try {  
  51.             FileInputStream fis = new FileInputStream(  
  52.                     "/sys/board_properties/virtualkeys." + deviceName);  
  53. /*这里就是读取kernel给出信息的地方,也就是地层与上层接口的地方,所以整个实现的重点就是这里*/  
  54.             InputStreamReader isr = new InputStreamReader(fis);  
  55.             BufferedReader br = new BufferedReader(isr, 2048);  
  56.             String str = br.readLine();  
  57.             if (str != null) {  
  58.                 String[] it = str.split(":");  
  59.                 if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "***** VIRTUAL KEYS: " + it);  
  60.                 final int N = it.length-6;  
  61.                 for (int i=0; i<=N; i+=6) {  
  62.                     if (!"0x01".equals(it[i])) {  
  63.                         Log.w(TAG, "Unknown virtual key type at elem #" + i  
  64.                                 + ": " + it[i]);  
  65.                         continue;  
  66.                     }  
  67.                     try {  
  68.                         VirtualKey sb = new VirtualKey();  
  69.                         sb.scancode = Integer.parseInt(it[i+1]);  
  70.                         sb.centerx = Integer.parseInt(it[i+2]);  
  71.                         sb.centery = Integer.parseInt(it[i+3]);  
  72.                         sb.width = Integer.parseInt(it[i+4]);  
  73.                         sb.height = Integer.parseInt(it[i+5]);  
  74.                         if (DEBUG_VIRTUAL_KEYS) Log.v(TAG, "Virtual key "  
  75.                                 + sb.scancode + ": center=" + sb.centerx + ","  
  76.                                 + sb.centery + " size=" + sb.width + "x"  
  77.                                 + sb.height);  
  78.                         mVirtualKeys.add(sb);  
  79.                     } catch (NumberFormatException e) {  
  80.                         Log.w(TAG, "Bad number at region " + i + " in: "  
  81.                                 + str, e);  
  82.                     }  
  83.                 }  
  84.             }  
  85.             br.close();  
  86.         } catch (FileNotFoundException e) {  
  87.             Log.i(TAG, "No virtual keys found");  
  88.         } catch (IOException e) {  
  89.             Log.w(TAG, "Error reading virtual keys", e);  
  90.         }  
  91.     }  

       总结 方案基本上就是这样,主要是调试工作可能需要一段时间,还有如果要做虚拟按键,还需要硬件的支持(超过显示区域的触摸屏区域)。本代码基于android 2.1请根据实际情况修改

原创粉丝点击